N = int(input())
edge = [[] for _ in range(N)]

for _ in range(N):
    l = list(map(int, input().split()))
    for j in range(l[1]):
        edge[l[0]-1].append(l[2+j]-1)


now_list = [0]
next_list = []
dist = 0
dists = [-1] * N
while len(now_list) > 0:
    for _ in range(len(now_list)):
        v = now_list.pop()
        if dists[v] == -1:
            dists[v] = dist
        else:
            continue
        for nv in edge[v]:
            next_list.append(nv)
    now_list = next_list
    next_list = []
    dist += 1

for i in range(N):
    print(1+i, dists[i])
 N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] edge = [[] for _ in range(N)] edge [[] for _ in range(N)] for _ in range(N):
    l = list(map(int, input().split()))
    for j in range(l[1]):
        edge[l[0]-1].append(l[2+j]-1) _ 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 for j in range(l[1]):
        edge[l[0]-1].append(l[2+j]-1) j range(l[1]) range l[1] l 1 edge[l[0]-1].append(l[2+j]-1) [l[0]-1].append [l[0]-1] edge l[0]-1 l[0] l 0 1 append l[2+j]-1 l[2+j] l 2+j 2 j 1 now_list = [0] now_list [0] 0 next_list = [] next_list [] dist = 0 dist 0 dists = [-1] * N dists [-1] * N [-1] -1 N while len(now_list) > 0:
    for _ in range(len(now_list)):
        v = now_list.pop()
        if dists[v] == -1:
            dists[v] = dist
        else:
            continue
        for nv in edge[v]:
            next_list.append(nv)
    now_list = next_list
    next_list = []
    dist += 1 len(now_list) > 0 len(now_list) len now_list 0 for _ in range(len(now_list)):
        v = now_list.pop()
        if dists[v] == -1:
            dists[v] = dist
        else:
            continue
        for nv in edge[v]:
            next_list.append(nv)
     _ range(len(now_list)) range len(now_list) len now_list v = now_list.pop() v now_list.pop() now_list.pop now_list pop if dists[v] == -1:
            dists[v] = dist
        else:
            continue
         dists[v] == -1 dists[v] dists v -1 dists[v] = dist dists[v] dists v dist continue for nv in edge[v]:
            next_list.append(nv)
     nv edge[v] edge v next_list.append(nv) next_list.append next_list append nv now_list = next_list now_list next_list next_list = [] next_list [] dist += 1 dist 1 for i in range(N):
    print(1+i, dists[i]) i range(N) range N print(1+i, dists[i]) print 1+i 1 i dists[i] dists i next_list = [] [] next_list now_list = next_list next_list now_list edge = [[] for _ in range(N)] [[] for _ in range(N)] edge dist = 0 0 dist next_list = [] [] next_list l = list(map(int, input().split())) list(map(int, input().split())) l dist += 1 1 dist N = int(input()) int(input()) N now_list = [0] [0] now_list dists = [-1] * N [-1] * N dists v = now_list.pop() now_list.pop() v
N = int(input())
edge = [[] for _ in range(N)]

for _ in range(N):
    l = list(map(int, input().split()))
    for j in range(l[1]):
        edge[l[0]-1].append(l[2+j]-1)


now_list = [0]
next_list = []
dist = 0
dists = [-1] * N
while len(now_list) > 0:
    for _ in range(len(now_list)):
        v = now_list.pop()
        if dists[v] == -1:
            dists[v] = dist
        else:
            continue
        for nv in edge[v]:
            next_list.append(nv)
    now_list = next_list
    next_list = []
    dist += 1

for i in range(N):
    print(1+i, dists[i])

