from collections import deque

n = int(input())
AL = [None for _ in range(n + 1)]
for i in range(1, n + 1):
    ukv = [int(x) for x in input().split()]
    AL[ukv[0]] = ukv[2:]

que = deque([1])
visited = [False] + [True] + [False] * (n - 1)
dist = [-1] + [0] + [-1] * (n - 1)
while que:
    u = que.popleft()
    for v in AL[u]:
        if visited[v]: continue
        dist[v] = dist[u] + 1
        visited[v] = True
        que.append(v)

for i in range(1, n + 1):
    print(i, dist[i]) from collections import deque n = int(input()) n int(input()) int input() input None for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) None AL = [None for _ in range(n + 1)] AL [None for _ in range(n + 1)] for i in range(1, n + 1):
    ukv = [int(x) for x in input().split()]
    AL[ukv[0]] = ukv[2:] i range(1, n + 1) range 1 n + 1 n 1 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x ukv = [int(x) for x in input().split()] ukv [int(x) for x in input().split()] AL[ukv[0]] = ukv[2:] AL[ukv[0]] AL ukv[0] ukv 0 ukv[2:] ukv 2: 2 que = deque([1]) que deque([1]) deque [1] 1 visited = [False] + [True] + [False] * (n - 1) visited [False] + [True] + [False] * (n - 1) [False] + [True] [False] False [True] True [False] * (n - 1) [False] False n - 1 n 1 dist = [-1] + [0] + [-1] * (n - 1) dist [-1] + [0] + [-1] * (n - 1) [-1] + [0] [-1] -1 [0] 0 [-1] * (n - 1) [-1] -1 n - 1 n 1 while que:
    u = que.popleft()
    for v in AL[u]:
        if visited[v]: continue
        dist[v] = dist[u] + 1
        visited[v] = True
        que.append(v) que u = que.popleft() u que.popleft() que.popleft que popleft for v in AL[u]:
        if visited[v]: continue
        dist[v] = dist[u] + 1
        visited[v] = True
        que.append(v) v AL[u] AL u if visited[v]: continue
         visited[v] visited v dist[v] = dist[u] + 1 dist[v] dist v dist[u] + 1 dist[u] dist u 1 visited[v] = True visited[v] visited v True que.append(v) que.append que append v for i in range(1, n + 1):
    print(i, dist[i]) i range(1, n + 1) range 1 n + 1 n 1 print(i, dist[i]) print i dist[i] dist i ukv = [int(x) for x in input().split()] [int(x) for x in input().split()] ukv que = deque([1]) deque([1]) que AL = [None for _ in range(n + 1)] [None for _ in range(n + 1)] AL visited = [False] + [True] + [False] * (n - 1) [False] + [True] + [False] * (n - 1) visited n = int(input()) int(input()) n u = que.popleft() que.popleft() u dist = [-1] + [0] + [-1] * (n - 1) [-1] + [0] + [-1] * (n - 1) dist
from collections import deque

n = int(input())
AL = [None for _ in range(n + 1)]
for i in range(1, n + 1):
    ukv = [int(x) for x in input().split()]
    AL[ukv[0]] = ukv[2:]

que = deque([1])
visited = [False] + [True] + [False] * (n - 1)
dist = [-1] + [0] + [-1] * (n - 1)
while que:
    u = que.popleft()
    for v in AL[u]:
        if visited[v]: continue
        dist[v] = dist[u] + 1
        visited[v] = True
        que.append(v)

for i in range(1, n + 1):
    print(i, dist[i])
