from collections import deque
N = int(input())
g = {i: deque() for i in range(1,N+1)} #1-index
for i in range(1, N+1):
    u, k, *v = map(int, input().split())
    for j in v:
        g[u].append(j)
        
seen = [0]*(N+1) #1-index
que = deque([])
dist = [-1]*(N+1) #1-index

seen[1] = 1
que.append(1)
dist[1] = 0

while que: #queが空になるまで
    q_no = que.popleft()
    if not g[q_no]: continue #点q-noから他の点に伸びていない
    
    for next_v in g[q_no]:
        if seen[next_v] == 1: continue #訪問済
        
        seen[next_v] = 1 #訪問済にする
        dist[next_v] = dist[q_no] + 1 #距離情報を格納
        que.append(next_v) #queに追加

for i in range(1, N+1):
    print(i, dist[i]) from collections import deque N = int(input()) N int(input()) int input() input i: deque() for i in range(1,N+1) for i in range(1,N+1) i range(1,N+1) range 1 N+1 N 1 for i in range(1,N+1) i deque() deque g = {i: deque() for i in range(1,N+1)} g {i: deque() for i in range(1,N+1)} for i in range(1, N+1):
    u, k, *v = map(int, input().split())
    for j in v:
        g[u].append(j)
         i range(1, N+1) range 1 N+1 N 1 u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v for j in v:
        g[u].append(j)
         j v g[u].append(j) [u].append [u] g u append j seen = [0]*(N+1) seen [0]*(N+1) [0] 0 N+1 N 1 que = deque([]) que deque([]) deque [] dist = [-1]*(N+1) dist [-1]*(N+1) [-1] -1 N+1 N 1 seen[1] = 1 seen[1] seen 1 1 que.append(1) que.append que append 1 dist[1] = 0 dist[1] dist 1 0 while que: #queが空になるまで
    q_no = que.popleft()
    if not g[q_no]: continue #点q-noから他の点に伸びていない
    
    for next_v in g[q_no]:
        if seen[next_v] == 1: continue #訪問済
        
        seen[next_v] = 1 #訪問済にする
        dist[next_v] = dist[q_no] + 1 #距離情報を格納
        que.append(next_v) #queに追加 que q_no = que.popleft() q_no que.popleft() que.popleft que popleft if not g[q_no]: continue #点q-noから他の点に伸びていない
    
     not g[q_no] g[q_no] g q_no for next_v in g[q_no]:
        if seen[next_v] == 1: continue #訪問済
        
        seen[next_v] = 1 #訪問済にする
        dist[next_v] = dist[q_no] + 1 #距離情報を格納
        que.append(next_v) #queに追加 next_v g[q_no] g q_no if seen[next_v] == 1: continue #訪問済
        
         seen[next_v] == 1 seen[next_v] seen next_v 1 seen[next_v] = 1 seen[next_v] seen next_v 1 dist[next_v] = dist[q_no] + 1 dist[next_v] dist next_v dist[q_no] + 1 dist[q_no] dist q_no 1 que.append(next_v) que.append que append next_v for i in range(1, N+1):
    print(i, dist[i]) i range(1, N+1) range 1 N+1 N 1 print(i, dist[i]) print i dist[i] dist i N = int(input()) int(input()) N dist = [-1]*(N+1) [-1]*(N+1) dist g = {i: deque() for i in range(1,N+1)} {i: deque() for i in range(1,N+1)} g que = deque([]) deque([]) que k, *v = map(int, input().split()) map(int, input().split()) k seen = [0]*(N+1) [0]*(N+1) seen q_no = que.popleft() que.popleft() q_no u, k, *v = map(int, input().split()) map(int, input().split()) u
from collections import deque
N = int(input())
g = {i: deque() for i in range(1,N+1)} #1-index
for i in range(1, N+1):
    u, k, *v = map(int, input().split())
    for j in v:
        g[u].append(j)
        
seen = [0]*(N+1) #1-index
que = deque([])
dist = [-1]*(N+1) #1-index

seen[1] = 1
que.append(1)
dist[1] = 0

while que: #queが空になるまで
    q_no = que.popleft()
    if not g[q_no]: continue #点q-noから他の点に伸びていない
    
    for next_v in g[q_no]:
        if seen[next_v] == 1: continue #訪問済
        
        seen[next_v] = 1 #訪問済にする
        dist[next_v] = dist[q_no] + 1 #距離情報を格納
        que.append(next_v) #queに追加

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