# -*- coding: utf-8 -*-

import sys
import os
import math


N = int(input())

E = []
E.append([])
d = [-1] * (N + 1)

for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E)

q = []
q.append(1)

visited = [False] * (N + 1)
d[1] = 0
visited[1] = True

while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

#print(d)
for i in range(1, N + 1):
    print(i, d[i]) import sys sys import os os import math math N = int(input()) N int(input()) int input() input E = [] E [] E.append([]) E.append E append [] d = [-1] * (N + 1) d [-1] * (N + 1) [-1] -1 N + 1 N 1 for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E) i range(N) range N lst = list(map(int, input().split())) lst list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split nodes = lst[2:] nodes lst[2:] lst 2: 2 E.append(nodes) E.append E append nodes q = [] q [] q.append(1) q.append q append 1 visited = [False] * (N + 1) visited [False] * (N + 1) [False] False N + 1 N 1 d[1] = 0 d[1] d 1 0 visited[1] = True visited[1] visited 1 True while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

#print(d) q index = q.pop(0) index q.pop(0) q.pop q pop 0 for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

#print(d) v E[index] E index if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

#print(d) not visited[v] visited[v] visited v q.append(v) q.append q append v d[v] = d[index] + 1 d[v] d v d[index] + 1 d[index] d index 1 visited[v] = True visited[v] visited v True for i in range(1, N + 1):
    print(i, d[i]) i range(1, N + 1) range 1 N + 1 N 1 print(i, d[i]) print i d[i] d i E = [] [] E q = [] [] q lst = list(map(int, input().split())) list(map(int, input().split())) lst d = [-1] * (N + 1) [-1] * (N + 1) d visited = [False] * (N + 1) [False] * (N + 1) visited N = int(input()) int(input()) N nodes = lst[2:] lst[2:] nodes index = q.pop(0) q.pop(0) index
# -*- coding: utf-8 -*-

import sys
import os
import math


N = int(input())

E = []
E.append([])
d = [-1] * (N + 1)

for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E)

q = []
q.append(1)

visited = [False] * (N + 1)
d[1] = 0
visited[1] = True

while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

#print(d)
for i in range(1, N + 1):
    print(i, d[i])