#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
from collections import deque

n=int(input())
G=[0]*n
memo=[0]*n
dis=[0]*n
dis[0]=1
que=deque()
que.append((0,1))

for i in range(n):
    inp=tuple(map(int,input().split()))
    G[inp[0]-1]=inp[2:]

while que:
    vertex,d=que.popleft()
    for v in G[vertex]:
        if not dis[v-1]:
            dis[v-1]=d+1
            que.append((v-1,d+1))
        

for i in range(n):
    print(i+1,dis[i]-1)
 import sys sys input = lambda: sys.stdin.readline()[:-1] input lambda: sys.stdin.readline()[:-1] sys.stdin.readline()[:-1] sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline :-1 -1 sys.setrecursionlimit(10**8) sys.setrecursionlimit sys setrecursionlimit 10**8 10 8 from collections import deque n=int(input()) n int(input()) int input() input G=[0]*n G [0]*n [0] 0 n memo=[0]*n memo [0]*n [0] 0 n dis=[0]*n dis [0]*n [0] 0 n dis[0]=1 dis[0] dis 0 1 que=deque() que deque() deque que.append((0,1)) que.append que append (0,1) 0 1 for i in range(n):
    inp=tuple(map(int,input().split()))
    G[inp[0]-1]=inp[2:] i range(n) range n inp=tuple(map(int,input().split())) inp tuple(map(int,input().split())) tuple map(int,input().split()) map int input().split() ().split () input split G[inp[0]-1]=inp[2:] G[inp[0]-1] G inp[0]-1 inp[0] inp 0 1 inp[2:] inp 2: 2 while que:
    vertex,d=que.popleft()
    for v in G[vertex]:
        if not dis[v-1]:
            dis[v-1]=d+1
            que.append((v-1,d+1))
         que vertex,d=que.popleft() vertex que.popleft() que.popleft que popleft d for v in G[vertex]:
        if not dis[v-1]:
            dis[v-1]=d+1
            que.append((v-1,d+1))
         v G[vertex] G vertex if not dis[v-1]:
            dis[v-1]=d+1
            que.append((v-1,d+1))
         not dis[v-1] dis[v-1] dis v-1 v 1 dis[v-1]=d+1 dis[v-1] dis v-1 v 1 d+1 d 1 que.append((v-1,d+1)) que.append que append (v-1,d+1) v-1 v 1 d+1 d 1 for i in range(n):
    print(i+1,dis[i]-1) i range(n) range n print(i+1,dis[i]-1) print i+1 i 1 dis[i]-1 dis[i] dis i 1 vertex,d=que.popleft() que.popleft() vertex memo=[0]*n [0]*n memo inp=tuple(map(int,input().split())) tuple(map(int,input().split())) inp dis=[0]*n [0]*n dis G=[0]*n [0]*n G input = lambda: sys.stdin.readline()[:-1] lambda: sys.stdin.readline()[:-1] input n=int(input()) int(input()) n que=deque() deque() que d=que.popleft() que.popleft() d
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
from collections import deque

n=int(input())
G=[0]*n
memo=[0]*n
dis=[0]*n
dis[0]=1
que=deque()
que.append((0,1))

for i in range(n):
    inp=tuple(map(int,input().split()))
    G[inp[0]-1]=inp[2:]

while que:
    vertex,d=que.popleft()
    for v in G[vertex]:
        if not dis[v-1]:
            dis[v-1]=d+1
            que.append((v-1,d+1))
        

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

