from collections import deque
n = int(input())
grap = [[]]
for i in range(n):
    u,k,*v = map(int,input().split())
    grap.append(v)

dis = [-1]*(n+1)
dis[0] = 0
dis[1] = 0

d = deque()
d.append(1)
while d:
    v = d.popleft()
    for i in grap[v]:
        if dis[i] != -1:
            continue
        dis[i] = dis[v] + 1
        d.append(i)

for i in range(1,n+1):
    print(i,dis[i])
 from collections import deque n = int(input()) n int(input()) int input() input grap = [[]] grap [[]] [] for i in range(n):
    u,k,*v = map(int,input().split())
    grap.append(v) i range(n) range n u,k,*v = map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split k *v v grap.append(v) grap.append grap append v dis = [-1]*(n+1) dis [-1]*(n+1) [-1] -1 n+1 n 1 dis[0] = 0 dis[0] dis 0 0 dis[1] = 0 dis[1] dis 1 0 d = deque() d deque() deque d.append(1) d.append d append 1 while d:
    v = d.popleft()
    for i in grap[v]:
        if dis[i] != -1:
            continue
        dis[i] = dis[v] + 1
        d.append(i) d v = d.popleft() v d.popleft() d.popleft d popleft for i in grap[v]:
        if dis[i] != -1:
            continue
        dis[i] = dis[v] + 1
        d.append(i) i grap[v] grap 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 d.append(i) d.append d append i for i in range(1,n+1):
    print(i,dis[i]) i range(1,n+1) range 1 n+1 n 1 print(i,dis[i]) print i dis[i] dis i d = deque() deque() d u,k,*v = map(int,input().split()) map(int,input().split()) u v = d.popleft() d.popleft() v n = int(input()) int(input()) n dis = [-1]*(n+1) [-1]*(n+1) dis grap = [[]] [[]] grap k,*v = map(int,input().split()) map(int,input().split()) k
from collections import deque
n = int(input())
grap = [[]]
for i in range(n):
    u,k,*v = map(int,input().split())
    grap.append(v)

dis = [-1]*(n+1)
dis[0] = 0
dis[1] = 0

d = deque()
d.append(1)
while d:
    v = d.popleft()
    for i in grap[v]:
        if dis[i] != -1:
            continue
        dis[i] = dis[v] + 1
        d.append(i)

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

