from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp) from collections import deque n = int(input()) n int(input()) int input() input A = [0]*(n+1) A [0]*(n+1) [0] 0 n+1 n 1 D = [-1]*(n+1) D [-1]*(n+1) [-1] -1 n+1 n 1 DP = [0]*(n+1) DP [0]*(n+1) [0] 0 n+1 n 1 d = deque() d deque() deque for i in range(n):
    A[i+1] = [int(i) for i in input().split()] i range(n) range n int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i A[i+1] = [int(i) for i in input().split()] A[i+1] A i+1 i 1 [int(i) for i in input().split()] d.append(1) d.append d append 1 D[1] = 0 D[1] D 1 0 DP[1] = 1 DP[1] DP 1 1 while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i) len(d)>0 len(d) len d 0 tmp = d.pop() tmp d.pop() d.pop d pop for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i) i A[tmp][2:] [tmp] A tmp 2: 2 if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i) not DP[i] DP[i] DP i D[i] = D[tmp]+1 D[i] D i D[tmp]+1 D[tmp] D tmp 1 DP[i] = 1 DP[i] DP i 1 d.appendleft(i) d.appendleft d appendleft i for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp) i range(1,n+1) range 1 n+1 n 1 tmp = "{} {}".format(i,D[i]) tmp "{} {}".format(i,D[i]) "{} {}".format "{} {}" format i D[i] D i print(tmp) print tmp A = [0]*(n+1) [0]*(n+1) A D = [-1]*(n+1) [-1]*(n+1) D d = deque() deque() d n = int(input()) int(input()) n tmp = d.pop() d.pop() tmp tmp = "{} {}".format(i,D[i]) "{} {}".format(i,D[i]) tmp DP = [0]*(n+1) [0]*(n+1) DP
from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)
