n=int(input())
ps=[0 for j in range(n+1)]

for i in range(n):
    p,d,*vs=[int(j) for j in input().split()]
    ps[p]=vs
    
dist=[-1 for i in range(n+1)]    
    
d=0
queue=[1]

while queue:
    pos=queue.pop(0)
    if dist[pos] == -1:
        dist[pos]=d
    for i in ps[pos]:
        if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1

for i in range(1,n+1):
    print(i,dist[i])
     n=int(input()) n int(input()) int input() input 0 for j in range(n+1) for j in range(n+1) j range(n+1) range n+1 n 1 for j in range(n+1) 0 ps=[0 for j in range(n+1)] ps [0 for j in range(n+1)] for i in range(n):
    p,d,*vs=[int(j) for j in input().split()]
    ps[p]=vs
     i range(n) range n int(j) for j in input().split() for j in input().split() j input().split() ().split () input split for j in input().split() int(j) int j p,d,*vs=[int(j) for j in input().split()] p [int(j) for j in input().split()] d *vs vs ps[p]=vs ps[p] ps p vs -1 for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) -1 dist=[-1 for i in range(n+1)] dist [-1 for i in range(n+1)] d=0 d 0 queue=[1] queue [1] 1 while queue:
    pos=queue.pop(0)
    if dist[pos] == -1:
        dist[pos]=d
    for i in ps[pos]:
        if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1 queue pos=queue.pop(0) pos queue.pop(0) queue.pop queue pop 0 if dist[pos] == -1:
        dist[pos]=d
     dist[pos] == -1 dist[pos] dist pos -1 dist[pos]=d dist[pos] dist pos d for i in ps[pos]:
        if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1 i ps[pos] ps pos if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1 dist[i]==-1 dist[i] dist i -1 queue.append(i) queue.append queue append i dist[i]=dist[pos]+1 dist[i] dist i dist[pos]+1 dist[pos] dist pos 1 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 d,*vs=[int(j) for j in input().split()] [int(j) for j in input().split()] d ps=[0 for j in range(n+1)] [0 for j in range(n+1)] ps d=0 0 d pos=queue.pop(0) queue.pop(0) pos dist=[-1 for i in range(n+1)] [-1 for i in range(n+1)] dist queue=[1] [1] queue p,d,*vs=[int(j) for j in input().split()] [int(j) for j in input().split()] p
n=int(input())
ps=[0 for j in range(n+1)]

for i in range(n):
    p,d,*vs=[int(j) for j in input().split()]
    ps[p]=vs
    
dist=[-1 for i in range(n+1)]    
    
d=0
queue=[1]

while queue:
    pos=queue.pop(0)
    if dist[pos] == -1:
        dist[pos]=d
    for i in ps[pos]:
        if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1

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