from collections import deque
import sys
input = sys.stdin.readline


N = int(input())
G = [[] for _ in range(N)]
for _ in range(N):
    ls = list(map(int, input().split()))
    u = ls[0] - 1
    for v in ls[2:]:
        G[u].append(v - 1)

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

while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

for v in range(N):
    print("{} {}".format(v + 1, dist[v])) from collections import deque import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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) [] G = [[] for _ in range(N)] G [[] for _ in range(N)] for _ in range(N):
    ls = list(map(int, input().split()))
    u = ls[0] - 1
    for v in ls[2:]:
        G[u].append(v - 1) _ range(N) range N ls = list(map(int, input().split())) ls list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = ls[0] - 1 u ls[0] - 1 ls[0] ls 0 1 for v in ls[2:]:
        G[u].append(v - 1) v ls[2:] ls 2: 2 G[u].append(v - 1) [u].append [u] G u append v - 1 v 1 dist = [-1] * N dist [-1] * N [-1] -1 N que = deque([0]) que deque([0]) deque [0] 0 dist[0] = 0 dist[0] dist 0 0 while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w) que v = que.popleft() v que.popleft() que.popleft que popleft d = dist[v] d dist[v] dist v for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w) w G[v] G v if dist[w] > -1:
            continue
         dist[w] > -1 dist[w] dist w -1 continue dist[w] = d + 1 dist[w] dist w d + 1 d 1 que.append(w) que.append que append w for v in range(N):
    print("{} {}".format(v + 1, dist[v])) v range(N) range N print("{} {}".format(v + 1, dist[v])) print "{} {}".format(v + 1, dist[v]) "{} {}".format "{} {}" format v + 1 v 1 dist[v] dist v input = sys.stdin.readline sys.stdin.readline input que = deque([0]) deque([0]) que G = [[] for _ in range(N)] [[] for _ in range(N)] G v = que.popleft() que.popleft() v d = dist[v] dist[v] d ls = list(map(int, input().split())) list(map(int, input().split())) ls N = int(input()) int(input()) N u = ls[0] - 1 ls[0] - 1 u dist = [-1] * N [-1] * N dist
from collections import deque
import sys
input = sys.stdin.readline


N = int(input())
G = [[] for _ in range(N)]
for _ in range(N):
    ls = list(map(int, input().split()))
    u = ls[0] - 1
    for v in ls[2:]:
        G[u].append(v - 1)

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

while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

for v in range(N):
    print("{} {}".format(v + 1, dist[v]))
