import sys

n = int(input())
G = [None]*n
for i in range(n):
    u, k, *vs = map(int, input().split())
    G[u-1] = [e-1 for e in vs]

from collections import deque
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: #already visited
            continue
        dist[w] = d + 1
        que.append(w)

for i in range(n):
    print(i+1, dist[i]) import sys sys n = int(input()) n int(input()) int input() input G = [None]*n G [None]*n [None] None n for i in range(n):
    u, k, *vs = map(int, input().split())
    G[u-1] = [e-1 for e in vs] 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 e-1 for e in vs for e in vs e vs for e in vs e-1 e 1 G[u-1] = [e-1 for e in vs] G[u-1] G u-1 u 1 [e-1 for e in vs] from collections import deque 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: #already visited
            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: #already visited
            continue
        dist[w] = d + 1
        que.append(w) w G[v] G v if dist[w] > -1: #already visited
            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 i in range(n):
    print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i que = deque([0]) deque([0]) que u, k, *vs = map(int, input().split()) map(int, input().split()) u k, *vs = map(int, input().split()) map(int, input().split()) k v = que.popleft() que.popleft() v d = dist[v] dist[v] d G = [None]*n [None]*n G n = int(input()) int(input()) n dist = [-1]*n [-1]*n dist
import sys

n = int(input())
G = [None]*n
for i in range(n):
    u, k, *vs = map(int, input().split())
    G[u-1] = [e-1 for e in vs]

from collections import deque
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: #already visited
            continue
        dist[w] = d + 1
        que.append(w)

for i in range(n):
    print(i+1, dist[i])
