from collections import defaultdict
v_num = int(input())
connect = defaultdict(list)
for v in range(v_num):
    inp = [int(n) - 1 for n in input().split(" ")]
    connect[inp[0]].extend(inp[2:])
answer = [[m + 1, -1] for m in range(v_num)]
queue = [0]
visited = [0 for n in range(v_num)]
visited[0] = 1
time = 0
while queue:
    new_queue = []
    for q in queue:
        answer[q][1] = time
        for c in connect[q]:
            if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
    queue = new_queue
    time += 1
for v in range(v_num):
    print(*answer[v]) from collections import defaultdict v_num = int(input()) v_num int(input()) int input() input connect = defaultdict(list) connect defaultdict(list) defaultdict list for v in range(v_num):
    inp = [int(n) - 1 for n in input().split(" ")]
    connect[inp[0]].extend(inp[2:]) v range(v_num) range v_num int(n) - 1 for n in input().split(" ") for n in input().split(" ") n input().split(" ") ().split () input split " " for n in input().split(" ") int(n) - 1 int(n) int n 1 inp = [int(n) - 1 for n in input().split(" ")] inp [int(n) - 1 for n in input().split(" ")] connect[inp[0]].extend(inp[2:]) [inp[0]].extend [inp[0]] connect inp[0] inp 0 extend inp[2:] inp 2: 2 [m + 1, -1] for m in range(v_num) for m in range(v_num) m range(v_num) range v_num for m in range(v_num) [m + 1, -1] m + 1 m 1 -1 answer = [[m + 1, -1] for m in range(v_num)] answer [[m + 1, -1] for m in range(v_num)] queue = [0] queue [0] 0 0 for n in range(v_num) for n in range(v_num) n range(v_num) range v_num for n in range(v_num) 0 visited = [0 for n in range(v_num)] visited [0 for n in range(v_num)] visited[0] = 1 visited[0] visited 0 1 time = 0 time 0 while queue:
    new_queue = []
    for q in queue:
        answer[q][1] = time
        for c in connect[q]:
            if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
    queue = new_queue
    time += 1 queue new_queue = [] new_queue [] for q in queue:
        answer[q][1] = time
        for c in connect[q]:
            if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
     q queue answer[q][1] = time answer[q][1] [q] answer q 1 time for c in connect[q]:
            if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
     c connect[q] connect q if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
     not visited[c] visited[c] visited c visited[c] = 1 visited[c] visited c 1 new_queue.append(c) new_queue.append new_queue append c queue = new_queue queue new_queue time += 1 time 1 for v in range(v_num):
    print(*answer[v]) v range(v_num) range v_num print(*answer[v]) print *answer[v] answer[v] answer v inp = [int(n) - 1 for n in input().split(" ")] [int(n) - 1 for n in input().split(" ")] inp time += 1 1 time answer = [[m + 1, -1] for m in range(v_num)] [[m + 1, -1] for m in range(v_num)] answer time = 0 0 time queue = new_queue new_queue queue visited = [0 for n in range(v_num)] [0 for n in range(v_num)] visited v_num = int(input()) int(input()) v_num connect = defaultdict(list) defaultdict(list) connect queue = [0] [0] queue new_queue = [] [] new_queue
from collections import defaultdict
v_num = int(input())
connect = defaultdict(list)
for v in range(v_num):
    inp = [int(n) - 1 for n in input().split(" ")]
    connect[inp[0]].extend(inp[2:])
answer = [[m + 1, -1] for m in range(v_num)]
queue = [0]
visited = [0 for n in range(v_num)]
visited[0] = 1
time = 0
while queue:
    new_queue = []
    for q in queue:
        answer[q][1] = time
        for c in connect[q]:
            if not visited[c]:
                visited[c] = 1
                new_queue.append(c)
    queue = new_queue
    time += 1
for v in range(v_num):
    print(*answer[v])