from collections import deque
n = int(input())

g = [[] for _ in range(n)]
for i in range(n):
    _, _, *edge = map(lambda x: int(x)-1, input().split())

    g[i] = edge

q = deque()
q.append(0)
dist = [-1 for _ in range(n)]
dist[0] = 0

while(len(q) > 0):
    v = q.popleft()

    for nv in g[v]:
        if(dist[nv] != -1): continue
        
        dist[nv] = dist[v] + 1
        q.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 [] 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):
    _, _, *edge = map(lambda x: int(x)-1, input().split())

    g[i] = edge i range(n) range n _, _, *edge = map(lambda x: int(x)-1, input().split()) _ map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split _ *edge edge g[i] = edge g[i] g i edge q = deque() q deque() deque q.append(0) q.append q append 0 -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 dist = [-1 for _ in range(n)] dist [-1 for _ in range(n)] dist[0] = 0 dist[0] dist 0 0 while(len(q) > 0):
    v = q.popleft()

    for nv in g[v]:
        if(dist[nv] != -1): continue
        
        dist[nv] = dist[v] + 1
        q.append(nv) len(q) > 0 len(q) len q 0 v = q.popleft() v q.popleft() q.popleft q popleft for nv in g[v]:
        if(dist[nv] != -1): continue
        
        dist[nv] = dist[v] + 1
        q.append(nv) nv g[v] g v if(dist[nv] != -1): continue
        
         dist[nv] != -1 dist[nv] dist nv -1 dist[nv] = dist[v] + 1 dist[nv] dist nv dist[v] + 1 dist[v] dist v 1 q.append(nv) q.append q 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 _, _, *edge = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) _ n = int(input()) int(input()) n _, *edge = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) _ v = q.popleft() q.popleft() v g = [[] for _ in range(n)] [[] for _ in range(n)] g q = deque() deque() q dist = [-1 for _ in range(n)] [-1 for _ in range(n)] dist
from collections import deque
n = int(input())

g = [[] for _ in range(n)]
for i in range(n):
    _, _, *edge = map(lambda x: int(x)-1, input().split())

    g[i] = edge

q = deque()
q.append(0)
dist = [-1 for _ in range(n)]
dist[0] = 0

while(len(q) > 0):
    v = q.popleft()

    for nv in g[v]:
        if(dist[nv] != -1): continue
        
        dist[nv] = dist[v] + 1
        q.append(nv)

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