from collections import defaultdict, deque

N = int(input())
edges = {}
scores = defaultdict(lambda: -1)

for _ in range(N):
    l = list(map(int, input().split()))
    node = l[0]
    degree = l[1]
    edges[node] = l[2:]

q = deque()
q.append((1, 0))

while q:
    node, score = q.popleft()
    if scores[node] != -1:
        continue
    scores[node] = score
    for child in edges[node]:
        q.append((child, score+1))

for node in range(1, N+1):
    print('%d %d' % (node, scores[node]))

 from collections import defaultdict, deque N = int(input()) N int(input()) int input() input edges = {} edges {} scores = defaultdict(lambda: -1) scores defaultdict(lambda: -1) defaultdict lambda: -1 -1 for _ in range(N):
    l = list(map(int, input().split()))
    node = l[0]
    degree = l[1]
    edges[node] = l[2:] _ 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 node = l[0] node l[0] l 0 degree = l[1] degree l[1] l 1 edges[node] = l[2:] edges[node] edges node l[2:] l 2: 2 q = deque() q deque() deque q.append((1, 0)) q.append q append (1, 0) 1 0 while q:
    node, score = q.popleft()
    if scores[node] != -1:
        continue
    scores[node] = score
    for child in edges[node]:
        q.append((child, score+1)) q node, score = q.popleft() node q.popleft() q.popleft q popleft score if scores[node] != -1:
        continue
     scores[node] != -1 scores[node] scores node -1 continue scores[node] = score scores[node] scores node score for child in edges[node]:
        q.append((child, score+1)) child edges[node] edges node q.append((child, score+1)) q.append q append (child, score+1) child score+1 score 1 for node in range(1, N+1):
    print('%d %d' % (node, scores[node])) node range(1, N+1) range 1 N+1 N 1 print('%d %d' % (node, scores[node])) print '%d %d' % (node, scores[node]) '%d %d' (node, scores[node]) node scores[node] scores node edges = {} {} edges q = deque() deque() q node = l[0] l[0] node score = q.popleft() q.popleft() score scores = defaultdict(lambda: -1) defaultdict(lambda: -1) scores N = int(input()) int(input()) N degree = l[1] l[1] degree l = list(map(int, input().split())) list(map(int, input().split())) l node, score = q.popleft() q.popleft() node
from collections import defaultdict, deque

N = int(input())
edges = {}
scores = defaultdict(lambda: -1)

for _ in range(N):
    l = list(map(int, input().split()))
    node = l[0]
    degree = l[1]
    edges[node] = l[2:]

q = deque()
q.append((1, 0))

while q:
    node, score = q.popleft()
    if scores[node] != -1:
        continue
    scores[node] = score
    for child in edges[node]:
        q.append((child, score+1))

for node in range(1, N+1):
    print('%d %d' % (node, scores[node]))


