from functools import lru_cache

@lru_cache(maxsize=None)
def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1)


N = int(input())
a = []
for _ in range(N):
    a.append([int(x) for x in input().split()])

ans = [[-1 for i in range(1)] for j in range(N)]

solve(0, 0)

for i, x in enumerate(ans):
    print(i + 1, *x)



 from functools import lru_cache def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1) solve global ans global a if count >= N:
        return
     count >= N count N return if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

     ans[i][0] == -1 ans[i][0] [i] ans i 0 -1 ans[i][0] = count ans[i][0] [i] ans i 0 count elif ans[i][0] > count:
        ans[i][0] = count

     ans[i][0] > count ans[i][0] [i] ans i 0 count ans[i][0] = count ans[i][0] [i] ans i 0 count for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1) x range(a[i][1]) range a[i][1] [i] a i 1 solve(a[i][2 + x] - 1, count + 1) solve a[i][2 + x] - 1 a[i][2 + x] [i] a i 2 + x 2 x 1 count + 1 count 1 i i count count @lru_cache(maxsize=None) solve maxsize=None @lru_cache(maxsize=None) lru_cache maxsize=None maxsize None solve N = int(input()) N int(input()) int input() input a = [] a [] for _ in range(N):
    a.append([int(x) for x in input().split()]) _ range(N) range N int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x a.append([int(x) for x in input().split()]) a.append a append [int(x) for x in input().split()] [-1 for i in range(1)] for j in range(N) for j in range(N) j range(N) range N for j in range(N) [-1 for i in range(1)] ans = [[-1 for i in range(1)] for j in range(N)] ans [[-1 for i in range(1)] for j in range(N)] solve(0, 0) solve 0 0 for i, x in enumerate(ans):
    print(i + 1, *x) i x enumerate(ans) enumerate ans print(i + 1, *x) print i + 1 i 1 *x x N = int(input()) int(input()) N ans = [[-1 for i in range(1)] for j in range(N)] [[-1 for i in range(1)] for j in range(N)] ans def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1) def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1) solve a = [] [] a
from functools import lru_cache

@lru_cache(maxsize=None)
def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1)


N = int(input())
a = []
for _ in range(N):
    a.append([int(x) for x in input().split()])

ans = [[-1 for i in range(1)] for j in range(N)]

solve(0, 0)

for i, x in enumerate(ans):
    print(i + 1, *x)




