from collections import deque
n = int(input())
edges = [[]] + [list(map(int, input().split())) for _ in range(n)]
inf = 10 ** 4
d = [inf for _ in range(n+1)]

que = deque()
d[1] = 0
que.append(1)
while que:
    cur = que.popleft()
    for to in edges[cur]:
        if d[to] == inf:
            d[to] = d[cur] + 1
            que.append(to)

for i in range(1, n+1):
    if d[i] == inf:
        d[i] = -1
    print(i, d[i])
 from collections import deque n = int(input()) n int(input()) int input() input list(map(int, input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split edges = [[]] + [list(map(int, input().split())) for _ in range(n)] edges [[]] + [list(map(int, input().split())) for _ in range(n)] [[]] [] [list(map(int, input().split())) for _ in range(n)] inf = 10 ** 4 inf 10 ** 4 10 4 inf for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) inf d = [inf for _ in range(n+1)] d [inf for _ in range(n+1)] que = deque() que deque() deque d[1] = 0 d[1] d 1 0 que.append(1) que.append que append 1 while que:
    cur = que.popleft()
    for to in edges[cur]:
        if d[to] == inf:
            d[to] = d[cur] + 1
            que.append(to) que cur = que.popleft() cur que.popleft() que.popleft que popleft for to in edges[cur]:
        if d[to] == inf:
            d[to] = d[cur] + 1
            que.append(to) to edges[cur] edges cur if d[to] == inf:
            d[to] = d[cur] + 1
            que.append(to) d[to] == inf d[to] d to inf d[to] = d[cur] + 1 d[to] d to d[cur] + 1 d[cur] d cur 1 que.append(to) que.append que append to for i in range(1, n+1):
    if d[i] == inf:
        d[i] = -1
    print(i, d[i]) i range(1, n+1) range 1 n+1 n 1 if d[i] == inf:
        d[i] = -1
     d[i] == inf d[i] d i inf d[i] = -1 d[i] d i -1 print(i, d[i]) print i d[i] d i edges = [[]] + [list(map(int, input().split())) for _ in range(n)] [[]] + [list(map(int, input().split())) for _ in range(n)] edges d = [inf for _ in range(n+1)] [inf for _ in range(n+1)] d inf = 10 ** 4 10 ** 4 inf cur = que.popleft() que.popleft() cur n = int(input()) int(input()) n que = deque() deque() que
from collections import deque
n = int(input())
edges = [[]] + [list(map(int, input().split())) for _ in range(n)]
inf = 10 ** 4
d = [inf for _ in range(n+1)]

que = deque()
d[1] = 0
que.append(1)
while que:
    cur = que.popleft()
    for to in edges[cur]:
        if d[to] == inf:
            d[to] = d[cur] + 1
            que.append(to)

for i in range(1, n+1):
    if d[i] == inf:
        d[i] = -1
    print(i, d[i])

