from collections import deque

n = int(input())

g = [[] for _ in range(n)]
for _ in range(n):
    u, k, *V = list(map(int, input().split()))
    V.sort()
    for v in V:
        g[u-1].append(v-1)
        # g[v-1].append(u-1)
d = [-1] * n
d[0] = 0

que = deque([])
que.append(0)
while que:
    v = que.popleft()
    for i in g[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i)

for i in range(n):
    print(f'{i+1} {d[i]}') 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 _ in range(n):
    u, k, *V = list(map(int, input().split()))
    V.sort()
    for v in V:
        g[u-1].append(v-1)
        # g[v-1].append(u-1) _ range(n) range n u, k, *V = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split k *V V V.sort() V.sort V sort for v in V:
        g[u-1].append(v-1)
        # g[v-1].append(u-1) v V g[u-1].append(v-1) [u-1].append [u-1] g u-1 u 1 append v-1 v 1 d = [-1] * n d [-1] * n [-1] -1 n d[0] = 0 d[0] d 0 0 que = deque([]) que deque([]) deque [] que.append(0) que.append que append 0 while que:
    v = que.popleft()
    for i in g[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i) que v = que.popleft() v que.popleft() que.popleft que popleft for i in g[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i) i g[v] g v if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i) d[i] == -1 d[i] d i -1 d[i] = d[v] + 1 d[i] d i d[v] + 1 d[v] d v 1 que.append(i) que.append que append i for i in range(n):
    print(f'{i+1} {d[i]}') i range(n) range n print(f'{i+1} {d[i]}') print f'{i+1} {d[i]}' d = [-1] * n [-1] * n d que = deque([]) deque([]) que u, k, *V = list(map(int, input().split())) list(map(int, input().split())) u k, *V = list(map(int, input().split())) list(map(int, input().split())) k n = int(input()) int(input()) n v = que.popleft() que.popleft() v g = [[] for _ in range(n)] [[] for _ in range(n)] g
from collections import deque

n = int(input())

g = [[] for _ in range(n)]
for _ in range(n):
    u, k, *V = list(map(int, input().split()))
    V.sort()
    for v in V:
        g[u-1].append(v-1)
        # g[v-1].append(u-1)
d = [-1] * n
d[0] = 0

que = deque([])
que.append(0)
while que:
    v = que.popleft()
    for i in g[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
            que.append(i)

for i in range(n):
    print(f'{i+1} {d[i]}')
