from collections import deque

n = int(input())
adj = [[]]
for i in range(n):
    adj.append(list(map(int, input().split()))[2:])

ans = [-1]*(n+1)
ans[1] = 0

q = deque([1])
visited = [False] * (n+1)
visited[1] = True

while q:
    x = q.popleft()
    for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True

for j in range(1, n+1):
    print(j, ans[j]) from collections import deque n = int(input()) n int(input()) int input() input adj = [[]] adj [[]] [] for i in range(n):
    adj.append(list(map(int, input().split()))[2:]) i range(n) range n adj.append(list(map(int, input().split()))[2:]) adj.append adj append list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 ans = [-1]*(n+1) ans [-1]*(n+1) [-1] -1 n+1 n 1 ans[1] = 0 ans[1] ans 1 0 q = deque([1]) q deque([1]) deque [1] 1 visited = [False] * (n+1) visited [False] * (n+1) [False] False n+1 n 1 visited[1] = True visited[1] visited 1 True while q:
    x = q.popleft()
    for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True q x = q.popleft() x q.popleft() q.popleft q popleft for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True y adj[x] adj x if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True visited[y] == False visited[y] visited y False q.append(y) q.append q append y ans[y] = ans[x]+1 ans[y] ans y ans[x]+1 ans[x] ans x 1 visited[y] = True visited[y] visited y True for j in range(1, n+1):
    print(j, ans[j]) j range(1, n+1) range 1 n+1 n 1 print(j, ans[j]) print j ans[j] ans j adj = [[]] [[]] adj visited = [False] * (n+1) [False] * (n+1) visited n = int(input()) int(input()) n ans = [-1]*(n+1) [-1]*(n+1) ans q = deque([1]) deque([1]) q x = q.popleft() q.popleft() x
from collections import deque

n = int(input())
adj = [[]]
for i in range(n):
    adj.append(list(map(int, input().split()))[2:])

ans = [-1]*(n+1)
ans[1] = 0

q = deque([1])
visited = [False] * (n+1)
visited[1] = True

while q:
    x = q.popleft()
    for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True

for j in range(1, n+1):
    print(j, ans[j])
