import sys
from collections import deque

n = int(sys.stdin.readline().strip())
edges = [[] for _ in range(n)]
for _ in range(n):
    tmp = list(map(int, sys.stdin.readline().strip().split(" ")))
    for node in tmp[2:]:
        edges[tmp[0] - 1].append(node-1)
# print(edges)

distance = [0] * n
q = deque()
q.append((0, 0))
visited = set()
while q:
    node, d = q.popleft()
    # print(node, d)
    if node in visited:
        continue
    distance[node] = d
    visited.add(node)
    for next in edges[node]:
        # print("next", next)
        q.append((next, d + 1))

for i, d in enumerate(distance):
    if i == 0:
        print(1, 0)
    else:
        print(i + 1, d if d > 0 else -1) import sys sys from collections import deque n = int(sys.stdin.readline().strip()) n int(sys.stdin.readline().strip()) int sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip [] 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):
    tmp = list(map(int, sys.stdin.readline().strip().split(" ")))
    for node in tmp[2:]:
        edges[tmp[0] - 1].append(node-1)
# print(edges) _ range(n) range n tmp = list(map(int, sys.stdin.readline().strip().split(" "))) tmp list(map(int, sys.stdin.readline().strip().split(" "))) list map(int, sys.stdin.readline().strip().split(" ")) map int sys.stdin.readline().strip().split(" ") sys.stdin.readline().strip().split sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip split " " for node in tmp[2:]:
        edges[tmp[0] - 1].append(node-1)
# print(edges) node tmp[2:] tmp 2: 2 edges[tmp[0] - 1].append(node-1) [tmp[0] - 1].append [tmp[0] - 1] edges tmp[0] - 1 tmp[0] tmp 0 1 append node-1 node 1 distance = [0] * n distance [0] * n [0] 0 n q = deque() q deque() deque q.append((0, 0)) q.append q append (0, 0) 0 0 visited = set() visited set() set while q:
    node, d = q.popleft()
    # print(node, d)
    if node in visited:
        continue
    distance[node] = d
    visited.add(node)
    for next in edges[node]:
        # print("next", next)
        q.append((next, d + 1)) q node, d = q.popleft() node q.popleft() q.popleft q popleft d if node in visited:
        continue
     node in visited node visited continue distance[node] = d distance[node] distance node d visited.add(node) visited.add visited add node for next in edges[node]:
        # print("next", next)
        q.append((next, d + 1)) next edges[node] edges node q.append((next, d + 1)) q.append q append (next, d + 1) next d + 1 d 1 for i, d in enumerate(distance):
    if i == 0:
        print(1, 0)
    else:
        print(i + 1, d if d > 0 else -1) i d enumerate(distance) enumerate distance if i == 0:
        print(1, 0)
    else:
        print(i + 1, d if d > 0 else -1) i == 0 i 0 print(1, 0) print 1 0 print(i + 1, d if d > 0 else -1) print i + 1 i 1 d if d > 0 else -1 d > 0 d 0 d -1 edges = [[] for _ in range(n)] [[] for _ in range(n)] edges d = q.popleft() q.popleft() d visited = set() set() visited distance = [0] * n [0] * n distance tmp = list(map(int, sys.stdin.readline().strip().split(" "))) list(map(int, sys.stdin.readline().strip().split(" "))) tmp q = deque() deque() q node, d = q.popleft() q.popleft() node n = int(sys.stdin.readline().strip()) int(sys.stdin.readline().strip()) n
import sys
from collections import deque

n = int(sys.stdin.readline().strip())
edges = [[] for _ in range(n)]
for _ in range(n):
    tmp = list(map(int, sys.stdin.readline().strip().split(" ")))
    for node in tmp[2:]:
        edges[tmp[0] - 1].append(node-1)
# print(edges)

distance = [0] * n
q = deque()
q.append((0, 0))
visited = set()
while q:
    node, d = q.popleft()
    # print(node, d)
    if node in visited:
        continue
    distance[node] = d
    visited.add(node)
    for next in edges[node]:
        # print("next", next)
        q.append((next, d + 1))

for i, d in enumerate(distance):
    if i == 0:
        print(1, 0)
    else:
        print(i + 1, d if d > 0 else -1)
