from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n = int(input())
d = [0 for i in range(n)]
f = [0 for i in range(n)]

colors = [] # 頂点の訪問状態
A = [[0 for j in range(n)] for i in range(n)] # 隣接行列
queue = deque([])

# 隣接行列の初期化
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK

bfs()

for i in range(n):
    print(i+1, d[i])
 from collections import deque from enum import Enum, auto class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto() Color WHITE = auto() WHITE auto() auto GRAY = auto() GRAY auto() auto BLACK = auto() BLACK auto() auto Enum n = int(input()) n int(input()) int input() input 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 d = [0 for i in range(n)] d [0 for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 f = [0 for i in range(n)] f [0 for i in range(n)] colors = [] colors [] [0 for j in range(n)] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0 for j in range(n)] A = [[0 for j in range(n)] for i in range(n)] A [[0 for j in range(n)] for i in range(n)] queue = deque([]) queue deque([]) deque [] for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1 i range(n) range n arr = list(map(int, input().split(' '))) arr list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' u, k, v = arr[0], arr[1], arr[2:] u arr[0] arr 0 k arr[1] arr 1 v arr[2:] arr 2: 2 for j in range(k):
        A[u-1][v[j]-1] = 1 j range(k) range k A[u-1][v[j]-1] = 1 A[u-1][v[j]-1] [u-1] A u-1 u 1 v[j]-1 v[j] v j 1 1 def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK bfs global colors, queue, d Color.WHITE for i in range(n) for i in range(n) i range(n) range n for i in range(n) Color.WHITE Color WHITE colors = [Color.WHITE for i in range(n)] colors [Color.WHITE for i in range(n)] -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 d = [-1 for i in range(n)] d [-1 for i in range(n)] colors[0] = Color.GRAY colors[0] colors 0 Color.GRAY Color GRAY d[0] = 0 d[0] d 0 0 queue.append(0) queue.append queue append 0 while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK len(queue) > 0 len(queue) len queue 0 u = queue.popleft() u queue.popleft() queue.popleft queue popleft for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
         v range(n) range n if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
         A[u][v] == 1 and colors[v] == Color.WHITE A[u][v] == 1 A[u][v] [u] A u v 1 colors[v] == Color.WHITE colors[v] colors v Color.WHITE Color WHITE colors[v] = Color.GRAY colors[v] colors v Color.GRAY Color GRAY d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 queue.append(v) queue.append queue append v colors[u] = Color.BLACK colors[u] colors u Color.BLACK Color BLACK bfs() bfs for i in range(n):
    print(i+1, d[i]) i range(n) range n print(i+1, d[i]) print i+1 i 1 d[i] d i n = int(input()) int(input()) n def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK bfs colors = [] [] colors A = [[0 for j in range(n)] for i in range(n)] [[0 for j in range(n)] for i in range(n)] A k, v = arr[0], arr[1], arr[2:] arr[1] k queue = deque([]) deque([]) queue u, k, v = arr[0], arr[1], arr[2:] arr[0] u d = [0 for i in range(n)] [0 for i in range(n)] d f = [0 for i in range(n)] [0 for i in range(n)] f arr = list(map(int, input().split(' '))) list(map(int, input().split(' '))) arr v = arr[0], arr[1], arr[2:] arr[2:] v class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto() class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto() Color
from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n = int(input())
d = [0 for i in range(n)]
f = [0 for i in range(n)]

colors = [] # 頂点の訪問状態
A = [[0 for j in range(n)] for i in range(n)] # 隣接行列
queue = deque([])

# 隣接行列の初期化
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK

bfs()

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

