from collections import deque
u = int(input())
g = [[i for i in map(int, input().split())] for _ in range(u)]
graph = [[] for _ in range(u)]
ans = [-1] * u
for i in range(u):
    for j in range(g[i][1]):
        graph[i].append(g[i][2 + j] - 1)
que = deque()
que.append(0)
ans[0] = 0
while que:
    v = que.popleft()
    for nv in graph[v]:
        if ans[nv] != -1:
            continue
        ans[nv] = ans[v] + 1
        que.append(nv)

for i in range(u):
    print(i+1, ans[i])
 from collections import deque u = int(input()) u int(input()) int input() input [i for i in map(int, input().split())] for _ in range(u) for _ in range(u) _ range(u) range u for _ in range(u) [i for i in map(int, input().split())] g = [[i for i in map(int, input().split())] for _ in range(u)] g [[i for i in map(int, input().split())] for _ in range(u)] [] for _ in range(u) for _ in range(u) _ range(u) range u for _ in range(u) [] graph = [[] for _ in range(u)] graph [[] for _ in range(u)] ans = [-1] * u ans [-1] * u [-1] -1 u for i in range(u):
    for j in range(g[i][1]):
        graph[i].append(g[i][2 + j] - 1) i range(u) range u for j in range(g[i][1]):
        graph[i].append(g[i][2 + j] - 1) j range(g[i][1]) range g[i][1] [i] g i 1 graph[i].append(g[i][2 + j] - 1) [i].append [i] graph i append g[i][2 + j] - 1 g[i][2 + j] [i] g i 2 + j 2 j 1 que = deque() que deque() deque que.append(0) que.append que append 0 ans[0] = 0 ans[0] ans 0 0 while que:
    v = que.popleft()
    for nv in graph[v]:
        if ans[nv] != -1:
            continue
        ans[nv] = ans[v] + 1
        que.append(nv) que v = que.popleft() v que.popleft() que.popleft que popleft for nv in graph[v]:
        if ans[nv] != -1:
            continue
        ans[nv] = ans[v] + 1
        que.append(nv) nv graph[v] graph v if ans[nv] != -1:
            continue
         ans[nv] != -1 ans[nv] ans nv -1 continue ans[nv] = ans[v] + 1 ans[nv] ans nv ans[v] + 1 ans[v] ans v 1 que.append(nv) que.append que append nv for i in range(u):
    print(i+1, ans[i]) i range(u) range u print(i+1, ans[i]) print i+1 i 1 ans[i] ans i que = deque() deque() que ans = [-1] * u [-1] * u ans g = [[i for i in map(int, input().split())] for _ in range(u)] [[i for i in map(int, input().split())] for _ in range(u)] g graph = [[] for _ in range(u)] [[] for _ in range(u)] graph u = int(input()) int(input()) u v = que.popleft() que.popleft() v
from collections import deque
u = int(input())
g = [[i for i in map(int, input().split())] for _ in range(u)]
graph = [[] for _ in range(u)]
ans = [-1] * u
for i in range(u):
    for j in range(g[i][1]):
        graph[i].append(g[i][2 + j] - 1)
que = deque()
que.append(0)
ans[0] = 0
while que:
    v = que.popleft()
    for nv in graph[v]:
        if ans[nv] != -1:
            continue
        ans[nv] = ans[v] + 1
        que.append(nv)

for i in range(u):
    print(i+1, ans[i])

