from collections import deque

N = int(input())
graph = {i:[] for i in range(N)}

for i in range(N):
    g = list(map(int, input().split()))
    for j in range(g[1]):
        graph[g[0]-1].append(g[j+2]-1)
        
que = deque()
dist = [-1 for _ in range(N)]
que.append(0)
dist[0] = 0

while que:
    top = que.popleft()
    
    for i in graph[top]:
        if(dist[i] != -1):
            continue
        dist[i] = dist[top]+1
        que.append(i)

for i in range(len(dist)):
    print(i+1, dist[i])     from collections import deque N = int(input()) N int(input()) int input() input i:[] for i in range(N) for i in range(N) i range(N) range N for i in range(N) i [] graph = {i:[] for i in range(N)} graph {i:[] for i in range(N)} for i in range(N):
    g = list(map(int, input().split()))
    for j in range(g[1]):
        graph[g[0]-1].append(g[j+2]-1)
         i range(N) range N g = list(map(int, input().split())) g list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for j in range(g[1]):
        graph[g[0]-1].append(g[j+2]-1)
         j range(g[1]) range g[1] g 1 graph[g[0]-1].append(g[j+2]-1) [g[0]-1].append [g[0]-1] graph g[0]-1 g[0] g 0 1 append g[j+2]-1 g[j+2] g j+2 j 2 1 que = deque() que deque() deque -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 dist = [-1 for _ in range(N)] dist [-1 for _ in range(N)] que.append(0) que.append que append 0 dist[0] = 0 dist[0] dist 0 0 while que:
    top = que.popleft()
    
    for i in graph[top]:
        if(dist[i] != -1):
            continue
        dist[i] = dist[top]+1
        que.append(i) que top = que.popleft() top que.popleft() que.popleft que popleft for i in graph[top]:
        if(dist[i] != -1):
            continue
        dist[i] = dist[top]+1
        que.append(i) i graph[top] graph top if(dist[i] != -1):
            continue
         dist[i] != -1 dist[i] dist i -1 continue dist[i] = dist[top]+1 dist[i] dist i dist[top]+1 dist[top] dist top 1 que.append(i) que.append que append i for i in range(len(dist)):
    print(i+1, dist[i])     i range(len(dist)) range len(dist) len dist print(i+1, dist[i]) print i+1 i 1 dist[i] dist i N = int(input()) int(input()) N que = deque() deque() que g = list(map(int, input().split())) list(map(int, input().split())) g graph = {i:[] for i in range(N)} {i:[] for i in range(N)} graph dist = [-1 for _ in range(N)] [-1 for _ in range(N)] dist top = que.popleft() que.popleft() top
from collections import deque

N = int(input())
graph = {i:[] for i in range(N)}

for i in range(N):
    g = list(map(int, input().split()))
    for j in range(g[1]):
        graph[g[0]-1].append(g[j+2]-1)
        
que = deque()
dist = [-1 for _ in range(N)]
que.append(0)
dist[0] = 0

while que:
    top = que.popleft()
    
    for i in graph[top]:
        if(dist[i] != -1):
            continue
        dist[i] = dist[top]+1
        que.append(i)

for i in range(len(dist)):
    print(i+1, dist[i])    
