import collections

N = int(input())
UKV = [list(map(int,input().split())) for _ in range(N)]

dist = [-1]*N
que = collections.deque()

G = [[]]*N
for ukv in UKV:
    G[ukv[0]-1] = sorted(ukv[2:])

dist[0] = 0
que.append(1)

while len(que) != 0:
    v = que.popleft()
    for nv in G[v-1]:
        if dist[nv-1] != -1:
            continue
        dist[nv-1] = dist[v-1] + 1
        que.append(nv)

for i,d in enumerate(dist):
    print(i+1, d) import collections collections N = int(input()) N int(input()) int input() input list(map(int,input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split UKV = [list(map(int,input().split())) for _ in range(N)] UKV [list(map(int,input().split())) for _ in range(N)] dist = [-1]*N dist [-1]*N [-1] -1 N que = collections.deque() que collections.deque() collections.deque collections deque G = [[]]*N G [[]]*N [[]] [] N for ukv in UKV:
    G[ukv[0]-1] = sorted(ukv[2:]) ukv UKV G[ukv[0]-1] = sorted(ukv[2:]) G[ukv[0]-1] G ukv[0]-1 ukv[0] ukv 0 1 sorted(ukv[2:]) sorted ukv[2:] ukv 2: 2 dist[0] = 0 dist[0] dist 0 0 que.append(1) que.append que append 1 while len(que) != 0:
    v = que.popleft()
    for nv in G[v-1]:
        if dist[nv-1] != -1:
            continue
        dist[nv-1] = dist[v-1] + 1
        que.append(nv) len(que) != 0 len(que) len que 0 v = que.popleft() v que.popleft() que.popleft que popleft for nv in G[v-1]:
        if dist[nv-1] != -1:
            continue
        dist[nv-1] = dist[v-1] + 1
        que.append(nv) nv G[v-1] G v-1 v 1 if dist[nv-1] != -1:
            continue
         dist[nv-1] != -1 dist[nv-1] dist nv-1 nv 1 -1 continue dist[nv-1] = dist[v-1] + 1 dist[nv-1] dist nv-1 nv 1 dist[v-1] + 1 dist[v-1] dist v-1 v 1 1 que.append(nv) que.append que append nv for i,d in enumerate(dist):
    print(i+1, d) i d enumerate(dist) enumerate dist print(i+1, d) print i+1 i 1 d G = [[]]*N [[]]*N G dist = [-1]*N [-1]*N dist que = collections.deque() collections.deque() que UKV = [list(map(int,input().split())) for _ in range(N)] [list(map(int,input().split())) for _ in range(N)] UKV v = que.popleft() que.popleft() v N = int(input()) int(input()) N
import collections

N = int(input())
UKV = [list(map(int,input().split())) for _ in range(N)]

dist = [-1]*N
que = collections.deque()

G = [[]]*N
for ukv in UKV:
    G[ukv[0]-1] = sorted(ukv[2:])

dist[0] = 0
que.append(1)

while len(que) != 0:
    v = que.popleft()
    for nv in G[v-1]:
        if dist[nv-1] != -1:
            continue
        dist[nv-1] = dist[v-1] + 1
        que.append(nv)

for i,d in enumerate(dist):
    print(i+1, d)
