from collections import deque

n = int(input())
C = [[] for _ in range(n+1)]
for _ in range(n):
    stdin = list(map(int,input().split()))
    for j in range(stdin[1]):
        C[stdin[0]].append(stdin[2+j])

q = deque()
q.append(1)
d = [-1]*(n+1)
visited = [False]*(n+1)
d[1] = 0
visited[1] = True

while q:
    node = q.popleft()
    for adj in C[node]:
        if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True

for i in range(1,n+1):
    print("{} {}".format(i,d[i]))









 from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] C = [[] for _ in range(n+1)] C [[] for _ in range(n+1)] for _ in range(n):
    stdin = list(map(int,input().split()))
    for j in range(stdin[1]):
        C[stdin[0]].append(stdin[2+j]) _ range(n) range n stdin = list(map(int,input().split())) stdin list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for j in range(stdin[1]):
        C[stdin[0]].append(stdin[2+j]) j range(stdin[1]) range stdin[1] stdin 1 C[stdin[0]].append(stdin[2+j]) [stdin[0]].append [stdin[0]] C stdin[0] stdin 0 append stdin[2+j] stdin 2+j 2 j q = deque() q deque() deque q.append(1) q.append q append 1 d = [-1]*(n+1) d [-1]*(n+1) [-1] -1 n+1 n 1 visited = [False]*(n+1) visited [False]*(n+1) [False] False n+1 n 1 d[1] = 0 d[1] d 1 0 visited[1] = True visited[1] visited 1 True while q:
    node = q.popleft()
    for adj in C[node]:
        if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True q node = q.popleft() node q.popleft() q.popleft q popleft for adj in C[node]:
        if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True adj C[node] C node if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True visited[adj] == False visited[adj] visited adj False q.append(adj) q.append q append adj d[adj] = d[node]+1 d[adj] d adj d[node]+1 d[node] d node 1 visited[adj] = True visited[adj] visited adj True for i in range(1,n+1):
    print("{} {}".format(i,d[i])) i range(1,n+1) range 1 n+1 n 1 print("{} {}".format(i,d[i])) print "{} {}".format(i,d[i]) "{} {}".format "{} {}" format i d[i] d i d = [-1]*(n+1) [-1]*(n+1) d visited = [False]*(n+1) [False]*(n+1) visited stdin = list(map(int,input().split())) list(map(int,input().split())) stdin n = int(input()) int(input()) n q = deque() deque() q node = q.popleft() q.popleft() node C = [[] for _ in range(n+1)] [[] for _ in range(n+1)] C
from collections import deque

n = int(input())
C = [[] for _ in range(n+1)]
for _ in range(n):
    stdin = list(map(int,input().split()))
    for j in range(stdin[1]):
        C[stdin[0]].append(stdin[2+j])

q = deque()
q.append(1)
d = [-1]*(n+1)
visited = [False]*(n+1)
d[1] = 0
visited[1] = True

while q:
    node = q.popleft()
    for adj in C[node]:
        if visited[adj] == False:
            q.append(adj)
            d[adj] = d[node]+1
            visited[adj] = True

for i in range(1,n+1):
    print("{} {}".format(i,d[i]))










