from collections import deque

INF = 1e9

def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d

N = int(input())
Adj = [None] * N

for i in range(N):
    u, k, *vertex = [int(i)-1 for i in input().split()]
    Adj[u] = vertex

distance = BFS(N, Adj)

for i, di in enumerate(distance, start = 1):
    if di == INF:
        print(i, -1)
    else:
        print(i, di) from collections import deque INF = 1e9 INF 1e9 def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d BFS d = [INF] * N d [INF] * N [INF] INF N d[0] = 0 d[0] d 0 0 next_v = deque([0]) next_v deque([0]) deque [0] 0 while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
     next_v u = next_v.popleft() u next_v.popleft() next_v.popleft next_v popleft for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
     v adj[u] adj u if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
     d[v] == INF d[v] d v INF d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 next_v.append(v) next_v.append next_v append v return d d n n adj adj N = int(input()) N int(input()) int input() input Adj = [None] * N Adj [None] * N [None] None N for i in range(N):
    u, k, *vertex = [int(i)-1 for i in input().split()]
    Adj[u] = vertex i range(N) range N int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 u, k, *vertex = [int(i)-1 for i in input().split()] u [int(i)-1 for i in input().split()] k *vertex vertex Adj[u] = vertex Adj[u] Adj u vertex distance = BFS(N, Adj) distance BFS(N, Adj) BFS N Adj for i, di in enumerate(distance, start = 1):
    if di == INF:
        print(i, -1)
    else:
        print(i, di) i di enumerate(distance, start = 1) enumerate distance start = 1 start 1 if di == INF:
        print(i, -1)
    else:
        print(i, di) di == INF di INF print(i, -1) print i -1 print(i, di) print i di INF = 1e9 1e9 INF distance = BFS(N, Adj) BFS(N, Adj) distance u, k, *vertex = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] u N = int(input()) int(input()) N def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d BFS k, *vertex = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] k Adj = [None] * N [None] * N Adj
from collections import deque

INF = 1e9

def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d

N = int(input())
Adj = [None] * N

for i in range(N):
    u, k, *vertex = [int(i)-1 for i in input().split()]
    Adj[u] = vertex

distance = BFS(N, Adj)

for i, di in enumerate(distance, start = 1):
    if di == INF:
        print(i, -1)
    else:
        print(i, di)