N = int(input())

tree = [[] for _ in range(N)]

for _ in range(N):
    u, k, *v = map(int, input().split())
    tree[u-1] = v

ans = [float('inf') for _ in range(N)]
ans[0] = 0

from collections import deque

q = deque([(0, 0)])

while q:
    p, d = q.popleft()
    for c in tree[p]:
        if ans[c-1] > d+1:
            ans[c-1] = d+1
            q.append((c-1, d+1))

for i in range(N):
    if ans[i] == float('inf'):
        print(i+1, -1)
    else:
        print(i+1, ans[i]) N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] tree = [[] for _ in range(N)] tree [[] for _ in range(N)] for _ in range(N):
    u, k, *v = map(int, input().split())
    tree[u-1] = v _ range(N) range N u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v tree[u-1] = v tree[u-1] tree u-1 u 1 v float('inf') for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) float('inf') float 'inf' ans = [float('inf') for _ in range(N)] ans [float('inf') for _ in range(N)] ans[0] = 0 ans[0] ans 0 0 from collections import deque q = deque([(0, 0)]) q deque([(0, 0)]) deque [(0, 0)] (0, 0) 0 0 while q:
    p, d = q.popleft()
    for c in tree[p]:
        if ans[c-1] > d+1:
            ans[c-1] = d+1
            q.append((c-1, d+1)) q p, d = q.popleft() p q.popleft() q.popleft q popleft d for c in tree[p]:
        if ans[c-1] > d+1:
            ans[c-1] = d+1
            q.append((c-1, d+1)) c tree[p] tree p if ans[c-1] > d+1:
            ans[c-1] = d+1
            q.append((c-1, d+1)) ans[c-1] > d+1 ans[c-1] ans c-1 c 1 d+1 d 1 ans[c-1] = d+1 ans[c-1] ans c-1 c 1 d+1 d 1 q.append((c-1, d+1)) q.append q append (c-1, d+1) c-1 c 1 d+1 d 1 for i in range(N):
    if ans[i] == float('inf'):
        print(i+1, -1)
    else:
        print(i+1, ans[i]) i range(N) range N if ans[i] == float('inf'):
        print(i+1, -1)
    else:
        print(i+1, ans[i]) ans[i] == float('inf') ans[i] ans i float('inf') float 'inf' print(i+1, -1) print i+1 i 1 -1 print(i+1, ans[i]) print i+1 i 1 ans[i] ans i k, *v = map(int, input().split()) map(int, input().split()) k d = q.popleft() q.popleft() d q = deque([(0, 0)]) deque([(0, 0)]) q tree = [[] for _ in range(N)] [[] for _ in range(N)] tree p, d = q.popleft() q.popleft() p N = int(input()) int(input()) N u, k, *v = map(int, input().split()) map(int, input().split()) u ans = [float('inf') for _ in range(N)] [float('inf') for _ in range(N)] ans
N = int(input())

tree = [[] for _ in range(N)]

for _ in range(N):
    u, k, *v = map(int, input().split())
    tree[u-1] = v

ans = [float('inf') for _ in range(N)]
ans[0] = 0

from collections import deque

q = deque([(0, 0)])

while q:
    p, d = q.popleft()
    for c in tree[p]:
        if ans[c-1] > d+1:
            ans[c-1] = d+1
            q.append((c-1, d+1))

for i in range(N):
    if ans[i] == float('inf'):
        print(i+1, -1)
    else:
        print(i+1, ans[i])
