from collections import deque
n = int(input())
G = [[] for _ in range(n)]
for i in range(n):
    u, k, *vs = map(int, input().split())
    u -= 1
    vs = list(map(lambda x: x - 1, vs))
    for v in vs:
        G[u].append(v)

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

while len(que) > 0:
    now_v = que.popleft()
    now_dist = dist[now_v]
    next_vs = G[now_v]
    for next_v in next_vs:
        if dist[next_v] == -1:
            dist[next_v] = now_dist + 1
            que.append(next_v)

for i, x in enumerate(dist):
    print(i + 1, x) 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) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for i in range(n):
    u, k, *vs = map(int, input().split())
    u -= 1
    vs = list(map(lambda x: x - 1, vs))
    for v in vs:
        G[u].append(v) i range(n) range n u, k, *vs = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *vs vs u -= 1 u 1 vs = list(map(lambda x: x - 1, vs)) vs list(map(lambda x: x - 1, vs)) list map(lambda x: x - 1, vs) map lambda x: x - 1 x - 1 x 1 x vs for v in vs:
        G[u].append(v) v vs G[u].append(v) [u].append [u] G u append v dist = [-1] * n dist [-1] * n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 que = deque([0]) que deque([0]) deque [0] 0 while len(que) > 0:
    now_v = que.popleft()
    now_dist = dist[now_v]
    next_vs = G[now_v]
    for next_v in next_vs:
        if dist[next_v] == -1:
            dist[next_v] = now_dist + 1
            que.append(next_v) len(que) > 0 len(que) len que 0 now_v = que.popleft() now_v que.popleft() que.popleft que popleft now_dist = dist[now_v] now_dist dist[now_v] dist now_v next_vs = G[now_v] next_vs G[now_v] G now_v for next_v in next_vs:
        if dist[next_v] == -1:
            dist[next_v] = now_dist + 1
            que.append(next_v) next_v next_vs if dist[next_v] == -1:
            dist[next_v] = now_dist + 1
            que.append(next_v) dist[next_v] == -1 dist[next_v] dist next_v -1 dist[next_v] = now_dist + 1 dist[next_v] dist next_v now_dist + 1 now_dist 1 que.append(next_v) que.append que append next_v for i, x in enumerate(dist):
    print(i + 1, x) i x enumerate(dist) enumerate dist print(i + 1, x) print i + 1 i 1 x vs = list(map(lambda x: x - 1, vs)) list(map(lambda x: x - 1, vs)) vs now_v = que.popleft() que.popleft() now_v u, k, *vs = map(int, input().split()) map(int, input().split()) u u -= 1 1 u k, *vs = map(int, input().split()) map(int, input().split()) k now_dist = dist[now_v] dist[now_v] now_dist que = deque([0]) deque([0]) que n = int(input()) int(input()) n G = [[] for _ in range(n)] [[] for _ in range(n)] G dist = [-1] * n [-1] * n dist next_vs = G[now_v] G[now_v] next_vs
from collections import deque
n = int(input())
G = [[] for _ in range(n)]
for i in range(n):
    u, k, *vs = map(int, input().split())
    u -= 1
    vs = list(map(lambda x: x - 1, vs))
    for v in vs:
        G[u].append(v)

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

while len(que) > 0:
    now_v = que.popleft()
    now_dist = dist[now_v]
    next_vs = G[now_v]
    for next_v in next_vs:
        if dist[next_v] == -1:
            dist[next_v] = now_dist + 1
            que.append(next_v)

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