from collections import deque

N = int(input())

Graph = {}
Node = []
visited = [-1]*(N)

for i in range(N):
    tmp = input().split()
    if int(tmp[1]) != 0:
        Graph[i] = [int(x)-1 for x in tmp[2:]]
    else:
        Graph[i] = []

queue = deque([0])
visited[0] = 0
while queue:
    x = queue.popleft()
    for i in Graph[x]:
        if visited[i] == -1:
            visited[i] = visited[x] + 1
            queue.append(i)

for i,j in enumerate(visited, 1):
    print(i, j) from collections import deque N = int(input()) N int(input()) int input() input Graph = {} Graph {} Node = [] Node [] visited = [-1]*(N) visited [-1]*(N) [-1] -1 N for i in range(N):
    tmp = input().split()
    if int(tmp[1]) != 0:
        Graph[i] = [int(x)-1 for x in tmp[2:]]
    else:
        Graph[i] = [] i range(N) range N tmp = input().split() tmp input().split() ().split () input split if int(tmp[1]) != 0:
        Graph[i] = [int(x)-1 for x in tmp[2:]]
    else:
        Graph[i] = [] int(tmp[1]) != 0 int(tmp[1]) int tmp[1] tmp 1 0 int(x)-1 for x in tmp[2:] for x in tmp[2:] x tmp[2:] tmp 2: 2 for x in tmp[2:] int(x)-1 int(x) int x 1 Graph[i] = [int(x)-1 for x in tmp[2:]] Graph[i] Graph i [int(x)-1 for x in tmp[2:]] Graph[i] = [] Graph[i] Graph i [] queue = deque([0]) queue deque([0]) deque [0] 0 visited[0] = 0 visited[0] visited 0 0 while queue:
    x = queue.popleft()
    for i in Graph[x]:
        if visited[i] == -1:
            visited[i] = visited[x] + 1
            queue.append(i) queue x = queue.popleft() x queue.popleft() queue.popleft queue popleft for i in Graph[x]:
        if visited[i] == -1:
            visited[i] = visited[x] + 1
            queue.append(i) i Graph[x] Graph x if visited[i] == -1:
            visited[i] = visited[x] + 1
            queue.append(i) visited[i] == -1 visited[i] visited i -1 visited[i] = visited[x] + 1 visited[i] visited i visited[x] + 1 visited[x] visited x 1 queue.append(i) queue.append queue append i for i,j in enumerate(visited, 1):
    print(i, j) i j enumerate(visited, 1) enumerate visited 1 print(i, j) print i j Node = [] [] Node Graph = {} {} Graph tmp = input().split() input().split() tmp x = queue.popleft() queue.popleft() x N = int(input()) int(input()) N queue = deque([0]) deque([0]) queue visited = [-1]*(N) [-1]*(N) visited
from collections import deque

N = int(input())

Graph = {}
Node = []
visited = [-1]*(N)

for i in range(N):
    tmp = input().split()
    if int(tmp[1]) != 0:
        Graph[i] = [int(x)-1 for x in tmp[2:]]
    else:
        Graph[i] = []

queue = deque([0])
visited[0] = 0
while queue:
    x = queue.popleft()
    for i in Graph[x]:
        if visited[i] == -1:
            visited[i] = visited[x] + 1
            queue.append(i)

for i,j in enumerate(visited, 1):
    print(i, j)
