from collections import deque

N = int(input())

edge = [[] for _ in range(N)]
for _ in range(N):
    t = list(map(int,input().split()))
    if t[1] != 0:
        edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:]))

dist = [-1]*N
dist[0] = 0
        
q = deque([0])
while q:
    now = q.popleft()
    for nxt in edge[now]:
        if dist[nxt] == -1:
            dist[nxt] = dist[now] + 1
            q.append(nxt)
        else:
            dist[nxt] = min(dist[nxt], dist[now] + 1)

for i,d in enumerate(dist):
    print(i+1, d) from collections import deque 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):
    t = list(map(int,input().split()))
    if t[1] != 0:
        edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:])) _ range(N) range N t = list(map(int,input().split())) t list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if t[1] != 0:
        edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:])) t[1] != 0 t[1] t 1 0 edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:])) edge[t[0]-1] edge t[0]-1 t[0] t 0 1 list(map(lambda x:int(x)-1, t[2:])) list map(lambda x:int(x)-1, t[2:]) map lambda x:int(x)-1 int(x)-1 int(x) int x 1 x t[2:] t 2: 2 dist = [-1]*N dist [-1]*N [-1] -1 N dist[0] = 0 dist[0] dist 0 0 q = deque([0]) q deque([0]) deque [0] 0 while q:
    now = q.popleft()
    for nxt in edge[now]:
        if dist[nxt] == -1:
            dist[nxt] = dist[now] + 1
            q.append(nxt)
        else:
            dist[nxt] = min(dist[nxt], dist[now] + 1) q now = q.popleft() now q.popleft() q.popleft q popleft for nxt in edge[now]:
        if dist[nxt] == -1:
            dist[nxt] = dist[now] + 1
            q.append(nxt)
        else:
            dist[nxt] = min(dist[nxt], dist[now] + 1) nxt edge[now] edge now if dist[nxt] == -1:
            dist[nxt] = dist[now] + 1
            q.append(nxt)
        else:
            dist[nxt] = min(dist[nxt], dist[now] + 1) dist[nxt] == -1 dist[nxt] dist nxt -1 dist[nxt] = dist[now] + 1 dist[nxt] dist nxt dist[now] + 1 dist[now] dist now 1 q.append(nxt) q.append q append nxt dist[nxt] = min(dist[nxt], dist[now] + 1) dist[nxt] dist nxt min(dist[nxt], dist[now] + 1) min dist[nxt] dist nxt dist[now] + 1 dist[now] dist now 1 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 dist = [-1]*N [-1]*N dist t = list(map(int,input().split())) list(map(int,input().split())) t N = int(input()) int(input()) N q = deque([0]) deque([0]) q now = q.popleft() q.popleft() now edge = [[] for _ in range(N)] [[] for _ in range(N)] edge
from collections import deque

N = int(input())

edge = [[] for _ in range(N)]
for _ in range(N):
    t = list(map(int,input().split()))
    if t[1] != 0:
        edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:]))

dist = [-1]*N
dist[0] = 0
        
q = deque([0])
while q:
    now = q.popleft()
    for nxt in edge[now]:
        if dist[nxt] == -1:
            dist[nxt] = dist[now] + 1
            q.append(nxt)
        else:
            dist[nxt] = min(dist[nxt], dist[now] + 1)

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