from collections import deque
graph = []
n = int(input())
dist = [[int(c),-1] for c in range(1,n+1)]
process_q = deque()
for i in range(n):
    graph.append([c-1 for c in list(map(int,input().split()))][2:])

def bfs():
    while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1

process_q.append(0)
dist[0][1] = 0
bfs()

for element in dist:
    print(*element)
 from collections import deque graph = [] graph [] n = int(input()) n int(input()) int input() input [int(c),-1] for c in range(1,n+1) for c in range(1,n+1) c range(1,n+1) range 1 n+1 n 1 for c in range(1,n+1) [int(c),-1] int(c) int c -1 dist = [[int(c),-1] for c in range(1,n+1)] dist [[int(c),-1] for c in range(1,n+1)] process_q = deque() process_q deque() deque for i in range(n):
    graph.append([c-1 for c in list(map(int,input().split()))][2:]) i range(n) range n c-1 for c in list(map(int,input().split())) for c in list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for c in list(map(int,input().split())) c-1 c 1 graph.append([c-1 for c in list(map(int,input().split()))][2:]) graph.append graph append [c-1 for c in list(map(int,input().split()))][2:] [c-1 for c in list(map(int,input().split()))] 2: 2 def bfs():
    while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1 bfs while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1 len(process_q) > 0 len(process_q) len process_q 0 parent = process_q.popleft() parent process_q.popleft() process_q.popleft process_q popleft for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1 child graph[parent] graph parent if dist[child][1] != -1:
                continue
             dist[child][1] != -1 dist[child][1] [child] dist child 1 -1 continue process_q.append(child) process_q.append process_q append child dist[child][1] = dist[parent][1] + 1 dist[child][1] [child] dist child 1 dist[parent][1] + 1 dist[parent][1] [parent] dist parent 1 1 process_q.append(0) process_q.append process_q append 0 dist[0][1] = 0 dist[0][1] [0] dist 0 1 0 bfs() bfs for element in dist:
    print(*element) element dist print(*element) print *element element process_q = deque() deque() process_q n = int(input()) int(input()) n def bfs():
    while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1 def bfs():
    while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1 bfs graph = [] [] graph dist = [[int(c),-1] for c in range(1,n+1)] [[int(c),-1] for c in range(1,n+1)] dist
from collections import deque
graph = []
n = int(input())
dist = [[int(c),-1] for c in range(1,n+1)]
process_q = deque()
for i in range(n):
    graph.append([c-1 for c in list(map(int,input().split()))][2:])

def bfs():
    while len(process_q) > 0:
        parent = process_q.popleft()
        for child in graph[parent]:
            if dist[child][1] != -1:
                continue
            process_q.append(child)
            dist[child][1] = dist[parent][1] + 1

process_q.append(0)
dist[0][1] = 0
bfs()

for element in dist:
    print(*element)

