from collections import deque

N = int(input())
tree = [[] for i in range(N)]

for _ in range(N):
    tmp = list(map(int, input().split()))
    for i in range(2, len(tmp)):
        tree[tmp[0]-1].append(tmp[i]-1)

q = deque(tree[0])
cnt = deque([1]*len(tree[0]))
s = {}
ans = [-1]*N

ans[0] = 0
s[0] = True
while len(q) != 0:
    g = q.popleft()
    c = cnt.popleft()
    if ans[g] == -1:
        ans[g] = c
        s[g] = True

    for tmp in tree[g]:
        if not(tmp in s):
            q.append(tmp)
            cnt.append(c+1)

for i in range(N):
    print(i+1, ans[i])
 from collections import deque N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] tree = [[] for i in range(N)] tree [[] for i in range(N)] for _ in range(N):
    tmp = list(map(int, input().split()))
    for i in range(2, len(tmp)):
        tree[tmp[0]-1].append(tmp[i]-1) _ range(N) range N tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for i in range(2, len(tmp)):
        tree[tmp[0]-1].append(tmp[i]-1) i range(2, len(tmp)) range 2 len(tmp) len tmp tree[tmp[0]-1].append(tmp[i]-1) [tmp[0]-1].append [tmp[0]-1] tree tmp[0]-1 tmp[0] tmp 0 1 append tmp[i]-1 tmp[i] tmp i 1 q = deque(tree[0]) q deque(tree[0]) deque tree[0] tree 0 cnt = deque([1]*len(tree[0])) cnt deque([1]*len(tree[0])) deque [1]*len(tree[0]) [1] 1 len(tree[0]) len tree[0] tree 0 s = {} s {} ans = [-1]*N ans [-1]*N [-1] -1 N ans[0] = 0 ans[0] ans 0 0 s[0] = True s[0] s 0 True while len(q) != 0:
    g = q.popleft()
    c = cnt.popleft()
    if ans[g] == -1:
        ans[g] = c
        s[g] = True

    for tmp in tree[g]:
        if not(tmp in s):
            q.append(tmp)
            cnt.append(c+1) len(q) != 0 len(q) len q 0 g = q.popleft() g q.popleft() q.popleft q popleft c = cnt.popleft() c cnt.popleft() cnt.popleft cnt popleft if ans[g] == -1:
        ans[g] = c
        s[g] = True

     ans[g] == -1 ans[g] ans g -1 ans[g] = c ans[g] ans g c s[g] = True s[g] s g True for tmp in tree[g]:
        if not(tmp in s):
            q.append(tmp)
            cnt.append(c+1) tmp tree[g] tree g if not(tmp in s):
            q.append(tmp)
            cnt.append(c+1) not(tmp in s) tmp in s tmp s q.append(tmp) q.append q append tmp cnt.append(c+1) cnt.append cnt append c+1 c 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 g = q.popleft() q.popleft() g c = cnt.popleft() cnt.popleft() c cnt = deque([1]*len(tree[0])) deque([1]*len(tree[0])) cnt tree = [[] for i in range(N)] [[] for i in range(N)] tree N = int(input()) int(input()) N q = deque(tree[0]) deque(tree[0]) q s = {} {} s ans = [-1]*N [-1]*N ans tmp = list(map(int, input().split())) list(map(int, input().split())) tmp
from collections import deque

N = int(input())
tree = [[] for i in range(N)]

for _ in range(N):
    tmp = list(map(int, input().split()))
    for i in range(2, len(tmp)):
        tree[tmp[0]-1].append(tmp[i]-1)

q = deque(tree[0])
cnt = deque([1]*len(tree[0]))
s = {}
ans = [-1]*N

ans[0] = 0
s[0] = True
while len(q) != 0:
    g = q.popleft()
    c = cnt.popleft()
    if ans[g] == -1:
        ans[g] = c
        s[g] = True

    for tmp in tree[g]:
        if not(tmp in s):
            q.append(tmp)
            cnt.append(c+1)

for i in range(N):
    print(i+1, ans[i])

