#!/usr/bin/env python3

from collections import deque

n = int(input())

edges = [[] for _ in range(n)]
for _ in range(n):
    line = list(map(int, input().split()))
    u = line[0] - 1
    if len(line) != 2:
        v_list = line[1:]
        for v in v_list:
            edges[u].append(v - 1)
# print(n, edges)

dist = [-1 for _ in range(n)]
que = deque()


def dfs(que, dist):
    while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj)


dist[0] = 0
que.appendleft(0)
dfs(que, dist)

for i in range(n):
    print(f"{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) [] edges = [[] for _ in range(n)] edges [[] for _ in range(n)] for _ in range(n):
    line = list(map(int, input().split()))
    u = line[0] - 1
    if len(line) != 2:
        v_list = line[1:]
        for v in v_list:
            edges[u].append(v - 1)
# print(n, edges) _ range(n) range n line = list(map(int, input().split())) line list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = line[0] - 1 u line[0] - 1 line[0] line 0 1 if len(line) != 2:
        v_list = line[1:]
        for v in v_list:
            edges[u].append(v - 1)
# print(n, edges) len(line) != 2 len(line) len line 2 v_list = line[1:] v_list line[1:] line 1: 1 for v in v_list:
            edges[u].append(v - 1)
# print(n, edges) v v_list edges[u].append(v - 1) [u].append [u] edges u append v - 1 v 1 -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)] que = deque() que deque() deque def dfs(que, dist):
    while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) dfs while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) len(que) > 0 len(que) len que 0 v = que.popleft() v que.popleft() que.popleft que popleft for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) v_adj edges[v] edges v if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) dist[v_adj] == -1 dist[v_adj] dist v_adj -1 dist[v_adj] = dist[v] + 1 dist[v_adj] dist v_adj dist[v] + 1 dist[v] dist v 1 que.append(v_adj) que.append que append v_adj que que dist dist dist[0] = 0 dist[0] dist 0 0 que.appendleft(0) que.appendleft que appendleft 0 dfs(que, dist) dfs que dist for i in range(n):
    print(f"{i + 1} {dist[i]}") i range(n) range n print(f"{i + 1} {dist[i]}") print f"{i + 1} {dist[i]}" line = list(map(int, input().split())) list(map(int, input().split())) line def dfs(que, dist):
    while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) def dfs(que, dist):
    while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj) dfs edges = [[] for _ in range(n)] [[] for _ in range(n)] edges que = deque() deque() que v_list = line[1:] line[1:] v_list dist = [-1 for _ in range(n)] [-1 for _ in range(n)] dist n = int(input()) int(input()) n u = line[0] - 1 line[0] - 1 u
#!/usr/bin/env python3

from collections import deque

n = int(input())

edges = [[] for _ in range(n)]
for _ in range(n):
    line = list(map(int, input().split()))
    u = line[0] - 1
    if len(line) != 2:
        v_list = line[1:]
        for v in v_list:
            edges[u].append(v - 1)
# print(n, edges)

dist = [-1 for _ in range(n)]
que = deque()


def dfs(que, dist):
    while len(que) > 0:
        v = que.popleft()

        for v_adj in edges[v]:
            if dist[v_adj] == -1:
                dist[v_adj] = dist[v] + 1
                que.append(v_adj)


dist[0] = 0
que.appendleft(0)
dfs(que, dist)

for i in range(n):
    print(f"{i + 1} {dist[i]}")

