from collections import deque
n = int(input())
G = [None for i in range(n)]

for i in range(n):
    u,k,*vs = map(int,input().split())
    G[u-1] = [e-1 for e in vs]


dist = [-1]*n #全頂点を未訪問に初期化(-1)で未訪問
que = deque([0])   #初期条件 (頂点 0 を初期ノードとする) 
dist[0] = 0 #0を訪問済み

while len(que)>0:
    now = que.popleft()
    for i in G[now]:
        if dist[i] != -1:
            continue
        dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(i)

for i in range(n):
    print(i+1,dist[i]) from collections import deque n = int(input()) n int(input()) int input() input None for i in range(n) for i in range(n) i range(n) range n for i in range(n) None G = [None for i in range(n)] G [None for i in range(n)] for i in range(n):
    u,k,*vs = map(int,input().split())
    G[u-1] = [e-1 for e in vs] i range(n) range n u,k,*vs = map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split k *vs vs e-1 for e in vs for e in vs e vs for e in vs e-1 e 1 G[u-1] = [e-1 for e in vs] G[u-1] G u-1 u 1 [e-1 for e in vs] dist = [-1]*n dist [-1]*n [-1] -1 n que = deque([0]) que deque([0]) deque [0] 0 dist[0] = 0 dist[0] dist 0 0 while len(que)>0:
    now = que.popleft()
    for i in G[now]:
        if dist[i] != -1:
            continue
        dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(i) len(que)>0 len(que) len que 0 now = que.popleft() now que.popleft() que.popleft que popleft for i in G[now]:
        if dist[i] != -1:
            continue
        dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(i) i G[now] G now if dist[i] != -1:
            continue
         dist[i] != -1 dist[i] dist i -1 continue dist[i] = dist[now] + 1 dist[i] dist i dist[now] + 1 dist[now] dist now 1 que.append(i) que.append que append i for i in range(n):
    print(i+1,dist[i]) i range(n) range n print(i+1,dist[i]) print i+1 i 1 dist[i] dist i now = que.popleft() que.popleft() now u,k,*vs = map(int,input().split()) map(int,input().split()) u G = [None for i in range(n)] [None for i in range(n)] G k,*vs = map(int,input().split()) map(int,input().split()) k que = deque([0]) deque([0]) que n = int(input()) int(input()) n dist = [-1]*n [-1]*n dist
from collections import deque
n = int(input())
G = [None for i in range(n)]

for i in range(n):
    u,k,*vs = map(int,input().split())
    G[u-1] = [e-1 for e in vs]


dist = [-1]*n #全頂点を未訪問に初期化(-1)で未訪問
que = deque([0])   #初期条件 (頂点 0 を初期ノードとする) 
dist[0] = 0 #0を訪問済み

while len(que)>0:
    now = que.popleft()
    for i in G[now]:
        if dist[i] != -1:
            continue
        dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(i)

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