from collections import deque

n = int(input())
graph = [list(map(int,input().split()))[2:] for _ in range(n)]

dist = [-1]*(n+1)
dist[0] = 0
dist[1] = 0
d = deque()
d.append(1)

while d:
    v = d.popleft()-1
    if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i)

for i in range(1,n+1):
    print(i,dist[i]) from collections import deque n = int(input()) n int(input()) int input() input list(map(int,input().split()))[2:] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int,input().split()))[2:] (map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 2: 2 graph = [list(map(int,input().split()))[2:] for _ in range(n)] graph [list(map(int,input().split()))[2:] for _ in range(n)] dist = [-1]*(n+1) dist [-1]*(n+1) [-1] -1 n+1 n 1 dist[0] = 0 dist[0] dist 0 0 dist[1] = 0 dist[1] dist 1 0 d = deque() d deque() deque d.append(1) d.append d append 1 while d:
    v = d.popleft()-1
    if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i) d v = d.popleft()-1 v d.popleft()-1 d.popleft() d.popleft d popleft 1 if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i) len(graph[v]) >= 1 len(graph[v]) len graph[v] graph v 1 for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i) i graph[v] graph v if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i) dist[i] == -1 dist[i] dist i -1 dist[i] = dist[v+1]+1 dist[i] dist i dist[v+1]+1 dist[v+1] dist v+1 v 1 1 d.append(i) d.append d append i for i in range(1,n+1):
    print(i,dist[i]) i range(1,n+1) range 1 n+1 n 1 print(i,dist[i]) print i dist[i] dist i d = deque() deque() d dist = [-1]*(n+1) [-1]*(n+1) dist n = int(input()) int(input()) n v = d.popleft()-1 d.popleft()-1 v graph = [list(map(int,input().split()))[2:] for _ in range(n)] [list(map(int,input().split()))[2:] for _ in range(n)] graph
from collections import deque

n = int(input())
graph = [list(map(int,input().split()))[2:] for _ in range(n)]

dist = [-1]*(n+1)
dist[0] = 0
dist[1] = 0
d = deque()
d.append(1)

while d:
    v = d.popleft()-1
    if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i)

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