import sys
import itertools
# import numpy as np
import time
import math
import heapq
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
# list(map(int, input().split()))

n = int(input())

adj = [[] for i in range(n)]

for i in range(n):
    line = list(map(int, input().split()))
    u = line[0]
    for j in range(line[1]):
        v = line[2 + j]
        adj[u - 1].append(v - 1)


# BFS
import queue
q = queue.Queue()
visited = [False] * n
dist = [-1] * n

visited[0] = True
dist[0] = 0
q.put(0)
while not q.empty():
    v = q.get()
    for u in adj[v]:
        if visited[u]:
            continue
        visited[u] = True
        dist[u] = dist[v] + 1
        q.put(u)

for i in range(n):
    val = dist[i]
    print(i + 1, val) import sys sys import itertools itertools import time time import math math import heapq heapq sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 from collections import defaultdict read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines n = int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] adj = [[] for i in range(n)] adj [[] for i in range(n)] for i in range(n):
    line = list(map(int, input().split()))
    u = line[0]
    for j in range(line[1]):
        v = line[2 + j]
        adj[u - 1].append(v - 1)


# BFS i range(n) range n line = list(map(int, input().split())) line list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = line[0] u line[0] line 0 for j in range(line[1]):
        v = line[2 + j]
        adj[u - 1].append(v - 1)


# BFS j range(line[1]) range line[1] line 1 v = line[2 + j] v line[2 + j] line 2 + j 2 j adj[u - 1].append(v - 1) [u - 1].append [u - 1] adj u - 1 u 1 append v - 1 v 1 import queue queue q = queue.Queue() q queue.Queue() queue.Queue queue Queue visited = [False] * n visited [False] * n [False] False n dist = [-1] * n dist [-1] * n [-1] -1 n visited[0] = True visited[0] visited 0 True dist[0] = 0 dist[0] dist 0 0 q.put(0) q.put q put 0 while not q.empty():
    v = q.get()
    for u in adj[v]:
        if visited[u]:
            continue
        visited[u] = True
        dist[u] = dist[v] + 1
        q.put(u) not q.empty() q.empty() q.empty q empty v = q.get() v q.get() q.get q get for u in adj[v]:
        if visited[u]:
            continue
        visited[u] = True
        dist[u] = dist[v] + 1
        q.put(u) u adj[v] adj v if visited[u]:
            continue
         visited[u] visited u continue visited[u] = True visited[u] visited u True dist[u] = dist[v] + 1 dist[u] dist u dist[v] + 1 dist[v] dist v 1 q.put(u) q.put q put u for i in range(n):
    val = dist[i]
    print(i + 1, val) i range(n) range n val = dist[i] val dist[i] dist i print(i + 1, val) print i + 1 i 1 val u = line[0] line[0] u readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines visited = [False] * n [False] * n visited line = list(map(int, input().split())) list(map(int, input().split())) line read = sys.stdin.buffer.read sys.stdin.buffer.read read readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline val = dist[i] dist[i] val q = queue.Queue() queue.Queue() q n = int(input()) int(input()) n v = line[2 + j] line[2 + j] v dist = [-1] * n [-1] * n dist v = q.get() q.get() v adj = [[] for i in range(n)] [[] for i in range(n)] adj
import sys
import itertools
# import numpy as np
import time
import math
import heapq
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
# list(map(int, input().split()))

n = int(input())

adj = [[] for i in range(n)]

for i in range(n):
    line = list(map(int, input().split()))
    u = line[0]
    for j in range(line[1]):
        v = line[2 + j]
        adj[u - 1].append(v - 1)


# BFS
import queue
q = queue.Queue()
visited = [False] * n
dist = [-1] * n

visited[0] = True
dist[0] = 0
q.put(0)
while not q.empty():
    v = q.get()
    for u in adj[v]:
        if visited[u]:
            continue
        visited[u] = True
        dist[u] = dist[v] + 1
        q.put(u)

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