import collections

N = int(input())
route = []
for i in range(N):
    a = list(map(int, input().split()))
    route.append(a[2:])
ans = [-1 for _ in range(N)]
ans[0] = 0
check = collections.deque([0])#now distance
while check:
    now = check.popleft()
    for next in route[now]:
        next -= 1
        if ans[next] == -1:
            check.append(next)
            ans[next] = ans[now]+1
for i in range(N):
    print(i+1, ans[i])
 import collections collections N = int(input()) N int(input()) int input() input route = [] route [] for i in range(N):
    a = list(map(int, input().split()))
    route.append(a[2:]) i range(N) range N a = list(map(int, input().split())) a list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split route.append(a[2:]) route.append route append a[2:] a 2: 2 -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 ans = [-1 for _ in range(N)] ans [-1 for _ in range(N)] ans[0] = 0 ans[0] ans 0 0 check = collections.deque([0]) check collections.deque([0]) collections.deque collections deque [0] 0 while check:
    now = check.popleft()
    for next in route[now]:
        next -= 1
        if ans[next] == -1:
            check.append(next)
            ans[next] = ans[now]+1 check now = check.popleft() now check.popleft() check.popleft check popleft for next in route[now]:
        next -= 1
        if ans[next] == -1:
            check.append(next)
            ans[next] = ans[now]+1 next route[now] route now next -= 1 next 1 if ans[next] == -1:
            check.append(next)
            ans[next] = ans[now]+1 ans[next] == -1 ans[next] ans next -1 check.append(next) check.append check append next ans[next] = ans[now]+1 ans[next] ans next ans[now]+1 ans[now] ans now 1 for i in range(N):
    print(i+1, ans[i]) i range(N) range N print(i+1, ans[i]) print i+1 i 1 ans[i] ans i check = collections.deque([0]) collections.deque([0]) check next -= 1 1 next now = check.popleft() check.popleft() now N = int(input()) int(input()) N route = [] [] route ans = [-1 for _ in range(N)] [-1 for _ in range(N)] ans a = list(map(int, input().split())) list(map(int, input().split())) a
import collections

N = int(input())
route = []
for i in range(N):
    a = list(map(int, input().split()))
    route.append(a[2:])
ans = [-1 for _ in range(N)]
ans[0] = 0
check = collections.deque([0])#now distance
while check:
    now = check.popleft()
    for next in route[now]:
        next -= 1
        if ans[next] == -1:
            check.append(next)
            ans[next] = ans[now]+1
for i in range(N):
    print(i+1, ans[i])

