from collections import deque

n = int(input())
points = [0 for i in range(n)]

class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False

for i in range(n):
    L = list(map(int, input().split()))
    points[i] = Point(i+1, L[1])
    if L[1] >= 1:
        points[i].destination = L[2:]

def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1]))

bfs(points[0])

for i in range(n):
    print("{} {}".format(i+1, points[i].distance)) from collections import deque n = int(input()) n int(input()) int input() input 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 points = [0 for i in range(n)] points [0 for i in range(n)] class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False Point def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False __init__ self.number = number self.number self number number self.arrow = arrow self.arrow self arrow arrow self.destination = [] self.destination self destination [] self.distance = -1 self.distance self distance -1 self.hadSearched = False self.hadSearched self hadSearched False self self number number arrow arrow for i in range(n):
    L = list(map(int, input().split()))
    points[i] = Point(i+1, L[1])
    if L[1] >= 1:
        points[i].destination = L[2:] i range(n) range n L = list(map(int, input().split())) L list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split points[i] = Point(i+1, L[1]) points[i] points i Point(i+1, L[1]) Point i+1 i 1 L[1] L 1 if L[1] >= 1:
        points[i].destination = L[2:] L[1] >= 1 L[1] L 1 1 points[i].destination = L[2:] points[i].destination [i] points i destination L[2:] L 2: 2 def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) bfs dq = deque() dq deque() deque coord.hadSearched = True coord.hadSearched coord hadSearched True dq.appendleft((0, coord)) dq.appendleft dq appendleft (0, coord) 0 coord while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) dq steps, po = dq.pop() steps dq.pop() dq.pop dq pop po po.distance = steps po.distance po distance steps if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) len(po.destination) > 0 len(po.destination) len po.destination po destination 0 for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) des po.destination po destination if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) points[int(des)-1].hadSearched == False points[int(des)-1].hadSearched [int(des)-1] points int(des)-1 int(des) int des 1 hadSearched False points[int(des)-1].hadSearched = True points[int(des)-1].hadSearched [int(des)-1] points int(des)-1 int(des) int des 1 hadSearched True dq.appendleft((steps+1, points[int(des)-1])) dq.appendleft dq appendleft (steps+1, points[int(des)-1]) steps+1 steps 1 points[int(des)-1] points int(des)-1 int(des) int des 1 coord coord bfs(points[0]) bfs points[0] points 0 for i in range(n):
    print("{} {}".format(i+1, points[i].distance)) i range(n) range n print("{} {}".format(i+1, points[i].distance)) print "{} {}".format(i+1, points[i].distance) "{} {}".format "{} {}" format i+1 i 1 points[i].distance [i] points i distance class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False Point points = [0 for i in range(n)] [0 for i in range(n)] points n = int(input()) int(input()) n L = list(map(int, input().split())) list(map(int, input().split())) L def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1])) bfs
from collections import deque

n = int(input())
points = [0 for i in range(n)]

class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False

for i in range(n):
    L = list(map(int, input().split()))
    points[i] = Point(i+1, L[1])
    if L[1] >= 1:
        points[i].destination = L[2:]

def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1]))

bfs(points[0])

for i in range(n):
    print("{} {}".format(i+1, points[i].distance))
