from collections import deque

import sys
sys.setrecursionlimit(1000000)

N=int(input())
graph = [[] for _ in range(N)] # 隣接リスト

for _ in range(N):
    v,c,*l = map(int,input().split())
    for i in l:
        graph[v-1].append(i-1)
        # graph[i-1].append(v-1)

dist=[-1]*N
dist[0]=0

d=deque()
d.append(0)

while d:
 # print(d)
 v = d.popleft()
 for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i)
for i in range(len(dist)):
    print(i+1,dist[i]) from collections import deque import sys sys sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 N=int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] for _ in range(N):
    v,c,*l = map(int,input().split())
    for i in l:
        graph[v-1].append(i-1)
        # graph[i-1].append(v-1) _ range(N) range N v,c,*l = map(int,input().split()) v map(int,input().split()) map int input().split() ().split () input split c *l l for i in l:
        graph[v-1].append(i-1)
        # graph[i-1].append(v-1) i l graph[v-1].append(i-1) [v-1].append [v-1] graph v-1 v 1 append i-1 i 1 dist=[-1]*N dist [-1]*N [-1] -1 N dist[0]=0 dist[0] dist 0 0 d=deque() d deque() deque d.append(0) d.append d append 0 while d:
 # print(d)
 v = d.popleft()
 for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i) d v = d.popleft() v d.popleft() d.popleft d popleft for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i) i graph[v] graph v if dist[i] != -1:
     continue
    dist[i] != -1 dist[i] dist i -1 continue dist[i] = dist[v] + 1 dist[i] dist i dist[v] + 1 dist[v] dist v 1 d.append(i) d.append d append i for i in range(len(dist)):
    print(i+1,dist[i]) i range(len(dist)) range len(dist) len dist print(i+1,dist[i]) print i+1 i 1 dist[i] dist i d=deque() deque() d graph = [[] for _ in range(N)] [[] for _ in range(N)] graph c,*l = map(int,input().split()) map(int,input().split()) c v,c,*l = map(int,input().split()) map(int,input().split()) v v = d.popleft() d.popleft() v N=int(input()) int(input()) N dist=[-1]*N [-1]*N dist
from collections import deque

import sys
sys.setrecursionlimit(1000000)

N=int(input())
graph = [[] for _ in range(N)] # 隣接リスト

for _ in range(N):
    v,c,*l = map(int,input().split())
    for i in l:
        graph[v-1].append(i-1)
        # graph[i-1].append(v-1)

dist=[-1]*N
dist[0]=0

d=deque()
d.append(0)

while d:
 # print(d)
 v = d.popleft()
 for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i)
for i in range(len(dist)):
    print(i+1,dist[i])
