from collections import deque

icase=0
if icase==0:
    n=int(input())
    u=[0]*n
#    g = [deque([]) for _ in range(n)]
    g = [[] for _ in range(n)]
    for i in range(n):
        ukv=list(map(int,input().split()))
        u[i]=ukv[0]-1
        ki=ukv[1]
        v=ukv[2:]
        for vi in v:
            g[u[i]].append(vi-1)
#            g[vi-1].append(u[i])
elif icase==1:
    n=4
    g = [[] for _ in range(n)]
    g[0]=[1,3]
    g[1]=[0,3]
    g[2]=[3]
    g[3]=[0,1,2]
#ans:0 1 2 1

q=deque([0])

stack=[0]
parent=[-1]*n
parent[0]=0
dist=[-1]*n
dist[0]=0
while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v])

for i in range(n):
    print(i+1,dist[i])
     from collections import deque icase=0 icase 0 if icase==0:
    n=int(input())
    u=[0]*n
#    g = [deque([]) for _ in range(n)]
    g = [[] for _ in range(n)]
    for i in range(n):
        ukv=list(map(int,input().split()))
        u[i]=ukv[0]-1
        ki=ukv[1]
        v=ukv[2:]
        for vi in v:
            g[u[i]].append(vi-1)
#            g[vi-1].append(u[i])
elif icase==1:
    n=4
    g = [[] for _ in range(n)]
    g[0]=[1,3]
    g[1]=[0,3]
    g[2]=[3]
    g[3]=[0,1,2]
#ans:0 1 2 1 icase==0 icase 0 n=int(input()) n int(input()) int input() input u=[0]*n u [0]*n [0] 0 n [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] g = [[] for _ in range(n)] g [[] for _ in range(n)] for i in range(n):
        ukv=list(map(int,input().split()))
        u[i]=ukv[0]-1
        ki=ukv[1]
        v=ukv[2:]
        for vi in v:
            g[u[i]].append(vi-1)
#            g[vi-1].append(u[i]) i range(n) range n ukv=list(map(int,input().split())) ukv list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split u[i]=ukv[0]-1 u[i] u i ukv[0]-1 ukv[0] ukv 0 1 ki=ukv[1] ki ukv[1] ukv 1 v=ukv[2:] v ukv[2:] ukv 2: 2 for vi in v:
            g[u[i]].append(vi-1)
#            g[vi-1].append(u[i]) vi v g[u[i]].append(vi-1) [u[i]].append [u[i]] g u[i] u i append vi-1 vi 1 elif icase==1:
    n=4
    g = [[] for _ in range(n)]
    g[0]=[1,3]
    g[1]=[0,3]
    g[2]=[3]
    g[3]=[0,1,2]
#ans:0 1 2 1 icase==1 icase 1 n=4 n 4 [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] g = [[] for _ in range(n)] g [[] for _ in range(n)] g[0]=[1,3] g[0] g 0 [1,3] 1 3 g[1]=[0,3] g[1] g 1 [0,3] 0 3 g[2]=[3] g[2] g 2 [3] 3 g[3]=[0,1,2] g[3] g 3 [0,1,2] 0 1 2 q=deque([0]) q deque([0]) deque [0] 0 stack=[0] stack [0] 0 parent=[-1]*n parent [-1]*n [-1] -1 n parent[0]=0 parent[0] parent 0 0 dist=[-1]*n dist [-1]*n [-1] -1 n dist[0]=0 dist[0] dist 0 0 while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v]) q state = q.popleft() state q.popleft() q.popleft q popleft for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v]) v g[state] g state if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v]) parent[v] !=-1 parent[v] parent v -1 continue parent[v]=state parent[v] parent v state dist[v]=dist[state]+1 dist[v] dist v dist[state]+1 dist[state] dist state 1 q.append(v) q.append q append v 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 n=int(input()) int(input()) n g = [[] for _ in range(n)] [[] for _ in range(n)] g v=ukv[2:] ukv[2:] v parent=[-1]*n [-1]*n parent ki=ukv[1] ukv[1] ki state = q.popleft() q.popleft() state g = [[] for _ in range(n)] [[] for _ in range(n)] g q=deque([0]) deque([0]) q n=4 4 n ukv=list(map(int,input().split())) list(map(int,input().split())) ukv icase=0 0 icase stack=[0] [0] stack u=[0]*n [0]*n u dist=[-1]*n [-1]*n dist
from collections import deque

icase=0
if icase==0:
    n=int(input())
    u=[0]*n
#    g = [deque([]) for _ in range(n)]
    g = [[] for _ in range(n)]
    for i in range(n):
        ukv=list(map(int,input().split()))
        u[i]=ukv[0]-1
        ki=ukv[1]
        v=ukv[2:]
        for vi in v:
            g[u[i]].append(vi-1)
#            g[vi-1].append(u[i])
elif icase==1:
    n=4
    g = [[] for _ in range(n)]
    g[0]=[1,3]
    g[1]=[0,3]
    g[2]=[3]
    g[3]=[0,1,2]
#ans:0 1 2 1

q=deque([0])

stack=[0]
parent=[-1]*n
parent[0]=0
dist=[-1]*n
dist[0]=0
while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v])

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