import collections
n = int(input())
adj_list = []

for _ in range(n):
    adj = list(map(int, input().split(" ")))
    adj_list.append([c - 1 for c in adj[2:]])

distances = [-1 for _ in range(n)]
queue = collections.deque()
queue.append(0)
distances[0] = 0

while queue:
    p = queue.popleft()

    for next_p in adj_list[p]:
        if distances[next_p] == -1:
            distances[next_p] = distances[p] + 1
            queue.append(next_p)

for i in range(n):
    print(i+1, distances[i]) import collections collections n = int(input()) n int(input()) int input() input adj_list = [] adj_list [] for _ in range(n):
    adj = list(map(int, input().split(" ")))
    adj_list.append([c - 1 for c in adj[2:]]) _ range(n) range n adj = list(map(int, input().split(" "))) adj list(map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " c - 1 for c in adj[2:] for c in adj[2:] c adj[2:] adj 2: 2 for c in adj[2:] c - 1 c 1 adj_list.append([c - 1 for c in adj[2:]]) adj_list.append adj_list append [c - 1 for c in adj[2:]] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 distances = [-1 for _ in range(n)] distances [-1 for _ in range(n)] queue = collections.deque() queue collections.deque() collections.deque collections deque queue.append(0) queue.append queue append 0 distances[0] = 0 distances[0] distances 0 0 while queue:
    p = queue.popleft()

    for next_p in adj_list[p]:
        if distances[next_p] == -1:
            distances[next_p] = distances[p] + 1
            queue.append(next_p) queue p = queue.popleft() p queue.popleft() queue.popleft queue popleft for next_p in adj_list[p]:
        if distances[next_p] == -1:
            distances[next_p] = distances[p] + 1
            queue.append(next_p) next_p adj_list[p] adj_list p if distances[next_p] == -1:
            distances[next_p] = distances[p] + 1
            queue.append(next_p) distances[next_p] == -1 distances[next_p] distances next_p -1 distances[next_p] = distances[p] + 1 distances[next_p] distances next_p distances[p] + 1 distances[p] distances p 1 queue.append(next_p) queue.append queue append next_p for i in range(n):
    print(i+1, distances[i]) i range(n) range n print(i+1, distances[i]) print i+1 i 1 distances[i] distances i adj_list = [] [] adj_list queue = collections.deque() collections.deque() queue p = queue.popleft() queue.popleft() p n = int(input()) int(input()) n distances = [-1 for _ in range(n)] [-1 for _ in range(n)] distances adj = list(map(int, input().split(" "))) list(map(int, input().split(" "))) adj
import collections
n = int(input())
adj_list = []

for _ in range(n):
    adj = list(map(int, input().split(" ")))
    adj_list.append([c - 1 for c in adj[2:]])

distances = [-1 for _ in range(n)]
queue = collections.deque()
queue.append(0)
distances[0] = 0

while queue:
    p = queue.popleft()

    for next_p in adj_list[p]:
        if distances[next_p] == -1:
            distances[next_p] = distances[p] + 1
            queue.append(next_p)

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