from collections import deque
n = int(input())
G = []
for _ in range(n):
    tmp = list(map(int, input().split()))
    u = tmp.pop(0)
    k = tmp.pop(0)
    t = []
    for i in range(k):
        a = tmp.pop(0)
        t.append(a-1)
    G.append(t)
    
dist = [-1]*n
dist[0] = 0
que = deque()
que.append(0)

while que:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] != -1:
            continue
        dist[nv] = dist[v] + 1
        que.append(nv)
    
for i in range(n):
    print(i+1, dist[i]) from collections import deque n = int(input()) n int(input()) int input() input G = [] G [] for _ in range(n):
    tmp = list(map(int, input().split()))
    u = tmp.pop(0)
    k = tmp.pop(0)
    t = []
    for i in range(k):
        a = tmp.pop(0)
        t.append(a-1)
    G.append(t)
     _ range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = tmp.pop(0) u tmp.pop(0) tmp.pop tmp pop 0 k = tmp.pop(0) k tmp.pop(0) tmp.pop tmp pop 0 t = [] t [] for i in range(k):
        a = tmp.pop(0)
        t.append(a-1)
     i range(k) range k a = tmp.pop(0) a tmp.pop(0) tmp.pop tmp pop 0 t.append(a-1) t.append t append a-1 a 1 G.append(t) G.append G append t dist = [-1]*n dist [-1]*n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 que = deque() que deque() deque que.append(0) que.append que append 0 while que:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] != -1:
            continue
        dist[nv] = dist[v] + 1
        que.append(nv)
     que v = que.popleft() v que.popleft() que.popleft que popleft for nv in G[v]:
        if dist[nv] != -1:
            continue
        dist[nv] = dist[v] + 1
        que.append(nv)
     nv G[v] G v if dist[nv] != -1:
            continue
         dist[nv] != -1 dist[nv] dist nv -1 continue dist[nv] = dist[v] + 1 dist[nv] dist nv dist[v] + 1 dist[v] dist v 1 que.append(nv) que.append que append nv 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 t = [] [] t a = tmp.pop(0) tmp.pop(0) a k = tmp.pop(0) tmp.pop(0) k n = int(input()) int(input()) n u = tmp.pop(0) tmp.pop(0) u G = [] [] G tmp = list(map(int, input().split())) list(map(int, input().split())) tmp v = que.popleft() que.popleft() v dist = [-1]*n [-1]*n dist que = deque() deque() que
from collections import deque
n = int(input())
G = []
for _ in range(n):
    tmp = list(map(int, input().split()))
    u = tmp.pop(0)
    k = tmp.pop(0)
    t = []
    for i in range(k):
        a = tmp.pop(0)
        t.append(a-1)
    G.append(t)
    
dist = [-1]*n
dist[0] = 0
que = deque()
que.append(0)

while que:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] != -1:
            continue
        dist[nv] = dist[v] + 1
        que.append(nv)
    
for i in range(n):
    print(i+1, dist[i])
