#AC

from collections import deque

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

#bfs(木でないかも)
L=[-1]*(n+1)
q=deque()

L[1]=0
q.append(1)
while q:
    now=q.popleft()
    L.append(now)
    for next in G[now]:
        #探索済み
        if L[next]>=0:
            continue
        #探索予定(なくて良い)
        #if next in q:
        #    continue
        q.append(next)
        L[next]=L[now]+1

for i in range(1,n+1):
    print(i,L[i])
 from collections import deque n=int(input()) n int(input()) int input() input G=[[]] G [[]] [] for i in range(n):
    G.append(list(map(int,input().split()))[2:])

#bfs(木でないかも) i range(n) range n G.append(list(map(int,input().split()))[2:]) G.append G 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 L=[-1]*(n+1) L [-1]*(n+1) [-1] -1 n+1 n 1 q=deque() q deque() deque L[1]=0 L[1] L 1 0 q.append(1) q.append q append 1 while q:
    now=q.popleft()
    L.append(now)
    for next in G[now]:
        #探索済み
        if L[next]>=0:
            continue
        #探索予定(なくて良い)
        #if next in q:
        #    continue
        q.append(next)
        L[next]=L[now]+1 q now=q.popleft() now q.popleft() q.popleft q popleft L.append(now) L.append L append now for next in G[now]:
        #探索済み
        if L[next]>=0:
            continue
        #探索予定(なくて良い)
        #if next in q:
        #    continue
        q.append(next)
        L[next]=L[now]+1 next G[now] G now if L[next]>=0:
            continue
        #探索予定(なくて良い)
        #if next in q:
        #    continue
         L[next]>=0 L[next] L next 0 continue q.append(next) q.append q append next L[next]=L[now]+1 L[next] L next L[now]+1 L[now] L now 1 for i in range(1,n+1):
    print(i,L[i]) i range(1,n+1) range 1 n+1 n 1 print(i,L[i]) print i L[i] L i L=[-1]*(n+1) [-1]*(n+1) L G=[[]] [[]] G now=q.popleft() q.popleft() now n=int(input()) int(input()) n q=deque() deque() q
#AC

from collections import deque

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

#bfs(木でないかも)
L=[-1]*(n+1)
q=deque()

L[1]=0
q.append(1)
while q:
    now=q.popleft()
    L.append(now)
    for next in G[now]:
        #探索済み
        if L[next]>=0:
            continue
        #探索予定(なくて良い)
        #if next in q:
        #    continue
        q.append(next)
        L[next]=L[now]+1

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

