#!/usr/bin/python3

def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
    
n = int(input())
# [isFind, d, f]
A = [[False, 0, 0] for i in range(n)]
U = []
V = []
dist = [-1] * n
for i in range(n):
    l = list(map(int, input().split()))
    U.append(l[0])
    V.append(l[2:])

find(1, V, 0, dist)

for u in U:
    print(u, dist[u - 1]) def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
     find i = id - 1 i id - 1 id 1 dist[i] = d dist[i] dist i d for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
     v V[i] V i if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
     dist[v - 1] == -1 or dist[v - 1] > d + 1 dist[v - 1] == -1 dist[v - 1] dist v - 1 v 1 -1 dist[v - 1] > d + 1 dist[v - 1] dist v - 1 v 1 d + 1 d 1 find(v, V, d + 1, dist) find v V d + 1 d 1 dist id id V V d d dist dist n = int(input()) n int(input()) int input() input [False, 0, 0] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [False, 0, 0] False 0 0 A = [[False, 0, 0] for i in range(n)] A [[False, 0, 0] for i in range(n)] U = [] U [] V = [] V [] dist = [-1] * n dist [-1] * n [-1] -1 n for i in range(n):
    l = list(map(int, input().split()))
    U.append(l[0])
    V.append(l[2:]) i range(n) range n l = list(map(int, input().split())) l list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split U.append(l[0]) U.append U append l[0] l 0 V.append(l[2:]) V.append V append l[2:] l 2: 2 find(1, V, 0, dist) find 1 V 0 dist for u in U:
    print(u, dist[u - 1]) u U print(u, dist[u - 1]) print u dist[u - 1] dist u - 1 u 1 def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
     def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
     find V = [] [] V dist = [-1] * n [-1] * n dist n = int(input()) int(input()) n A = [[False, 0, 0] for i in range(n)] [[False, 0, 0] for i in range(n)] A l = list(map(int, input().split())) list(map(int, input().split())) l U = [] [] U
#!/usr/bin/python3

def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        if dist[v - 1] == -1 or dist[v - 1] > d + 1:
            find(v, V, d + 1, dist)
    
n = int(input())
# [isFind, d, f]
A = [[False, 0, 0] for i in range(n)]
U = []
V = []
dist = [-1] * n
for i in range(n):
    l = list(map(int, input().split()))
    U.append(l[0])
    V.append(l[2:])

find(1, V, 0, dist)

for u in U:
    print(u, dist[u - 1])