import sys
from collections import deque

sys.setrecursionlimit(50000)

n = int(input())

data = [[] for _ in range(n)]
answer = [-1]*n
answer[0] = 0
for i in range(n):
    line = list(map(int, input().split(" ")))
    if line[1] != 0:
        for e in line[2:]:
            data[i].append(e)


def bfs(vs, count=0):
    next_vs = []
    for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
    if not next_vs == []:
        bfs(next_vs, count+1)


bfs([1])

for i in range(n):
    line = str(i+1) + " " + str(answer[i])
    print(line)
 import sys sys from collections import deque sys.setrecursionlimit(50000) sys.setrecursionlimit sys setrecursionlimit 50000 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) [] data = [[] for _ in range(n)] data [[] for _ in range(n)] answer = [-1]*n answer [-1]*n [-1] -1 n answer[0] = 0 answer[0] answer 0 0 for i in range(n):
    line = list(map(int, input().split(" ")))
    if line[1] != 0:
        for e in line[2:]:
            data[i].append(e) i range(n) range n line = list(map(int, input().split(" "))) line list(map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " if line[1] != 0:
        for e in line[2:]:
            data[i].append(e) line[1] != 0 line[1] line 1 0 for e in line[2:]:
            data[i].append(e) e line[2:] line 2: 2 data[i].append(e) [i].append [i] data i append e def bfs(vs, count=0):
    next_vs = []
    for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
    if not next_vs == []:
        bfs(next_vs, count+1) bfs next_vs = [] next_vs [] for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
     v vs for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
     n_v data[v-1] data v-1 v 1 if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
     answer[n_v-1] == -1 answer[n_v-1] answer n_v-1 n_v 1 -1 answer[n_v-1] = count+1 answer[n_v-1] answer n_v-1 n_v 1 count+1 count 1 next_vs.append(n_v) next_vs.append next_vs append n_v if not next_vs == []:
        bfs(next_vs, count+1) not next_vs == [] next_vs == [] next_vs [] bfs(next_vs, count+1) bfs next_vs count+1 count 1 vs vs count=0 count 0 bfs([1]) bfs [1] 1 for i in range(n):
    line = str(i+1) + " " + str(answer[i])
    print(line) i range(n) range n line = str(i+1) + " " + str(answer[i]) line str(i+1) + " " + str(answer[i]) str(i+1) + " " str(i+1) str i+1 i 1 " " str(answer[i]) str answer[i] answer i print(line) print line data = [[] for _ in range(n)] [[] for _ in range(n)] data n = int(input()) int(input()) n line = str(i+1) + " " + str(answer[i]) str(i+1) + " " + str(answer[i]) line answer = [-1]*n [-1]*n answer line = list(map(int, input().split(" "))) list(map(int, input().split(" "))) line def bfs(vs, count=0):
    next_vs = []
    for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
    if not next_vs == []:
        bfs(next_vs, count+1) def bfs(vs, count=0):
    next_vs = []
    for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
    if not next_vs == []:
        bfs(next_vs, count+1) bfs
import sys
from collections import deque

sys.setrecursionlimit(50000)

n = int(input())

data = [[] for _ in range(n)]
answer = [-1]*n
answer[0] = 0
for i in range(n):
    line = list(map(int, input().split(" ")))
    if line[1] != 0:
        for e in line[2:]:
            data[i].append(e)


def bfs(vs, count=0):
    next_vs = []
    for v in vs:
        for n_v in data[v-1]:
            if answer[n_v-1] == -1:
                answer[n_v-1] = count+1
                next_vs.append(n_v)
    if not next_vs == []:
        bfs(next_vs, count+1)


bfs([1])

for i in range(n):
    line = str(i+1) + " " + str(answer[i])
    print(line)

