#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy
setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
gragh=[[] for _ in range(n)]
q=deque()
for i in range(n):
    u=list(map(int,input().rstrip().split()))
    for j in u[2:]:
        gragh[u[0]-1].append(j-1)
#print(gragh)
dis=[-1]*n
dis[0]=0
q.append(0)
while len(q)!=0:
    v=q.popleft()
    for i in gragh[v]:
        if dis[i]!=-1:
            continue
        dis[i]=dis[v]+1
        q.append(i)
for i,j in enumerate(dis,1):
    print(i,j) from collections import Counter,deque from sys import stdin,setrecursionlimit setrecursionlimit(10**6) setrecursionlimit 10**6 10 6 input=stdin.readline input stdin.readline stdin readline n=int(input().rstrip()) n int(input().rstrip()) int input().rstrip() ().rstrip () input rstrip [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] gragh=[[] for _ in range(n)] gragh [[] for _ in range(n)] q=deque() q deque() deque for i in range(n):
    u=list(map(int,input().rstrip().split()))
    for j in u[2:]:
        gragh[u[0]-1].append(j-1)
#print(gragh) i range(n) range n u=list(map(int,input().rstrip().split())) u list(map(int,input().rstrip().split())) list map(int,input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split for j in u[2:]:
        gragh[u[0]-1].append(j-1)
#print(gragh) j u[2:] u 2: 2 gragh[u[0]-1].append(j-1) [u[0]-1].append [u[0]-1] gragh u[0]-1 u[0] u 0 1 append j-1 j 1 dis=[-1]*n dis [-1]*n [-1] -1 n dis[0]=0 dis[0] dis 0 0 q.append(0) q.append q append 0 while len(q)!=0:
    v=q.popleft()
    for i in gragh[v]:
        if dis[i]!=-1:
            continue
        dis[i]=dis[v]+1
        q.append(i) len(q)!=0 len(q) len q 0 v=q.popleft() v q.popleft() q.popleft q popleft for i in gragh[v]:
        if dis[i]!=-1:
            continue
        dis[i]=dis[v]+1
        q.append(i) i gragh[v] gragh v if dis[i]!=-1:
            continue
         dis[i]!=-1 dis[i] dis i -1 continue dis[i]=dis[v]+1 dis[i] dis i dis[v]+1 dis[v] dis v 1 q.append(i) q.append q append i for i,j in enumerate(dis,1):
    print(i,j) i j enumerate(dis,1) enumerate dis 1 print(i,j) print i j gragh=[[] for _ in range(n)] [[] for _ in range(n)] gragh dis=[-1]*n [-1]*n dis u=list(map(int,input().rstrip().split())) list(map(int,input().rstrip().split())) u q=deque() deque() q input=stdin.readline stdin.readline input v=q.popleft() q.popleft() v n=int(input().rstrip()) int(input().rstrip()) n
#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy
setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
gragh=[[] for _ in range(n)]
q=deque()
for i in range(n):
    u=list(map(int,input().rstrip().split()))
    for j in u[2:]:
        gragh[u[0]-1].append(j-1)
#print(gragh)
dis=[-1]*n
dis[0]=0
q.append(0)
while len(q)!=0:
    v=q.popleft()
    for i in gragh[v]:
        if dis[i]!=-1:
            continue
        dis[i]=dis[v]+1
        q.append(i)
for i,j in enumerate(dis,1):
    print(i,j)
