n = int(input())
d = [-1]*(n+1)
adjacent_list = [[] for _ in range(n+1)]
for _ in range(n):
    edge = list(map(int,input().split()))
    if len(edge) >= 3:
        adjacent_list[edge[0]].extend(edge[2:])

from collections import deque

que = deque()
que.append(1)
d[1] = 0
while len(que) != 0:
    v = que.popleft()
    for nv in adjacent_list[v]:
        if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
        que.append(nv)

for i,dist in enumerate(d):
    if i == 0:
        continue
    print(i,dist) n = int(input()) n int(input()) int input() input d = [-1]*(n+1) d [-1]*(n+1) [-1] -1 n+1 n 1 [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] adjacent_list = [[] for _ in range(n+1)] adjacent_list [[] for _ in range(n+1)] for _ in range(n):
    edge = list(map(int,input().split()))
    if len(edge) >= 3:
        adjacent_list[edge[0]].extend(edge[2:]) _ range(n) range n edge = list(map(int,input().split())) edge list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if len(edge) >= 3:
        adjacent_list[edge[0]].extend(edge[2:]) len(edge) >= 3 len(edge) len edge 3 adjacent_list[edge[0]].extend(edge[2:]) [edge[0]].extend [edge[0]] adjacent_list edge[0] edge 0 extend edge[2:] edge 2: 2 from collections import deque que = deque() que deque() deque que.append(1) que.append que append 1 d[1] = 0 d[1] d 1 0 while len(que) != 0:
    v = que.popleft()
    for nv in adjacent_list[v]:
        if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
        que.append(nv) len(que) != 0 len(que) len que 0 v = que.popleft() v que.popleft() que.popleft que popleft for nv in adjacent_list[v]:
        if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
        que.append(nv) nv adjacent_list[v] adjacent_list v if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
         d[nv] == -1 d[nv] d nv -1 d[nv] = d[v] + 1 d[nv] d nv d[v] + 1 d[v] d v 1 continue que.append(nv) que.append que append nv for i,dist in enumerate(d):
    if i == 0:
        continue
    print(i,dist) i dist enumerate(d) enumerate d if i == 0:
        continue
     i == 0 i 0 continue print(i,dist) print i dist n = int(input()) int(input()) n d = [-1]*(n+1) [-1]*(n+1) d adjacent_list = [[] for _ in range(n+1)] [[] for _ in range(n+1)] adjacent_list que = deque() deque() que edge = list(map(int,input().split())) list(map(int,input().split())) edge v = que.popleft() que.popleft() v
n = int(input())
d = [-1]*(n+1)
adjacent_list = [[] for _ in range(n+1)]
for _ in range(n):
    edge = list(map(int,input().split()))
    if len(edge) >= 3:
        adjacent_list[edge[0]].extend(edge[2:])

from collections import deque

que = deque()
que.append(1)
d[1] = 0
while len(que) != 0:
    v = que.popleft()
    for nv in adjacent_list[v]:
        if d[nv] == -1:
            d[nv] = d[v] + 1
        else:
            continue
        que.append(nv)

for i,dist in enumerate(d):
    if i == 0:
        continue
    print(i,dist)
