n = int(input())
G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)]
# print(G)
from collections import deque
d = deque([1])
dist  = [10**10 for _ in range(n+1)]
dist[1] = 0
while len(d) != 0:
    now = d.popleft()
    # print(now)
    for nexte in G[now]:
        if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte)
for i,d in enumerate(dist[1:]):
    if d != 10**10:
        print(i+1,d)
    else:
        print(i+1,-1)

        
 n = int(input()) n int(input()) int input() input list(map(int, input().split()))[2:] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)] G [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)] [[0]] [0] 0 [list(map(int, input().split()))[2:] for _ in range(n)] from collections import deque d = deque([1]) d deque([1]) deque [1] 1 10**10 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) 10**10 10 10 dist  = [10**10 for _ in range(n+1)] dist [10**10 for _ in range(n+1)] dist[1] = 0 dist[1] dist 1 0 while len(d) != 0:
    now = d.popleft()
    # print(now)
    for nexte in G[now]:
        if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte) len(d) != 0 len(d) len d 0 now = d.popleft() now d.popleft() d.popleft d popleft for nexte in G[now]:
        if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte) nexte G[now] G now if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte) dist[now] +1 < dist[nexte] dist[now] +1 dist[now] dist now 1 dist[nexte] dist nexte dist[nexte] = dist[now] + 1 dist[nexte] dist nexte dist[now] + 1 dist[now] dist now 1 d.append(nexte) d.append d append nexte for i,d in enumerate(dist[1:]):
    if d != 10**10:
        print(i+1,d)
    else:
        print(i+1,-1)

         i d enumerate(dist[1:]) enumerate dist[1:] dist 1: 1 if d != 10**10:
        print(i+1,d)
    else:
        print(i+1,-1)

         d != 10**10 d 10**10 10 10 print(i+1,d) print i+1 i 1 d print(i+1,-1) print i+1 i 1 -1 dist  = [10**10 for _ in range(n+1)] [10**10 for _ in range(n+1)] dist d = deque([1]) deque([1]) d G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)] [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)] G n = int(input()) int(input()) n now = d.popleft() d.popleft() now
n = int(input())
G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)]
# print(G)
from collections import deque
d = deque([1])
dist  = [10**10 for _ in range(n+1)]
dist[1] = 0
while len(d) != 0:
    now = d.popleft()
    # print(now)
    for nexte in G[now]:
        if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte)
for i,d in enumerate(dist[1:]):
    if d != 10**10:
        print(i+1,d)
    else:
        print(i+1,-1)

        

