# -*- coding: utf-8 -*-
from collections import deque

N = int(input())
adj = [[0]*N for _ in range(N)]
unsearched = set(range(1, N))
result = {n:-1 for n in range(N)}

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

que = deque([[0, 0]]) # edge distance


while len(que):
    q = que.pop()
    result[q[0]] = q[1]
    next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)]
    unsearched = unsearched.difference([i[0] for i in next_])
    que.extendleft(next_)

for n in range(N):
    print("{} {}".format(n+1, result[n])) from collections import deque N = int(input()) N int(input()) int input() input [0]*N for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0]*N [0] 0 N adj = [[0]*N for _ in range(N)] adj [[0]*N for _ in range(N)] unsearched = set(range(1, N)) unsearched set(range(1, N)) set range(1, N) range 1 N n:-1 for n in range(N) for n in range(N) n range(N) range N for n in range(N) n -1 result = {n:-1 for n in range(N)} result {n:-1 for n in range(N)} for n in range(N):
    inp = list(map(int, input().split()))
    for i in inp[2:]:
        adj[inp[0]-1][i-1] = 1 n range(N) range N inp = list(map(int, input().split())) inp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for i in inp[2:]:
        adj[inp[0]-1][i-1] = 1 i inp[2:] inp 2: 2 adj[inp[0]-1][i-1] = 1 adj[inp[0]-1][i-1] [inp[0]-1] adj inp[0]-1 inp[0] inp 0 1 i-1 i 1 1 que = deque([[0, 0]]) que deque([[0, 0]]) deque [[0, 0]] [0, 0] 0 0 while len(que):
    q = que.pop()
    result[q[0]] = q[1]
    next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)]
    unsearched = unsearched.difference([i[0] for i in next_])
    que.extendleft(next_) len(que) len que q = que.pop() q que.pop() que.pop que pop result[q[0]] = q[1] result[q[0]] result q[0] q 0 q[1] q 1 [i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched) for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched) i a enumerate(adj[q[0]]) enumerate adj[q[0]] adj q[0] q 0 (a==1) & (i in unsearched) a==1 a 1 i in unsearched i unsearched if (a==1) & (i in unsearched) [i, q[1] + 1] i q[1] + 1 q[1] q 1 1 next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)] next_ [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)] i[0] for i in next_ for i in next_ i next_ for i in next_ i[0] i 0 unsearched = unsearched.difference([i[0] for i in next_]) unsearched unsearched.difference([i[0] for i in next_]) unsearched.difference unsearched difference [i[0] for i in next_] que.extendleft(next_) que.extendleft que extendleft next_ for n in range(N):
    print("{} {}".format(n+1, result[n])) n range(N) range N print("{} {}".format(n+1, result[n])) print "{} {}".format(n+1, result[n]) "{} {}".format "{} {}" format n+1 n 1 result[n] result n inp = list(map(int, input().split())) list(map(int, input().split())) inp q = que.pop() que.pop() q next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)] [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)] next_ unsearched = set(range(1, N)) set(range(1, N)) unsearched que = deque([[0, 0]]) deque([[0, 0]]) que adj = [[0]*N for _ in range(N)] [[0]*N for _ in range(N)] adj result = {n:-1 for n in range(N)} {n:-1 for n in range(N)} result N = int(input()) int(input()) N unsearched = unsearched.difference([i[0] for i in next_]) unsearched.difference([i[0] for i in next_]) unsearched
# -*- coding: utf-8 -*-
from collections import deque

N = int(input())
adj = [[0]*N for _ in range(N)]
unsearched = set(range(1, N))
result = {n:-1 for n in range(N)}

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

que = deque([[0, 0]]) # edge distance


while len(que):
    q = que.pop()
    result[q[0]] = q[1]
    next_ = [[i, q[1] + 1] for i, a in enumerate(adj[q[0]]) if (a==1) & (i in unsearched)]
    unsearched = unsearched.difference([i[0] for i in next_])
    que.extendleft(next_)

for n in range(N):
    print("{} {}".format(n+1, result[n]))