from collections import deque
V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v)
dist = [-1] * V
dist[0] = 0
que = deque([0])

while len(que):
    v = que.popleft()
    for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c)
for i, d in enumerate(dist):
    print(i+1, d) from collections import deque V = int(input()) V int(input()) int input() input [] for _ in range(V) for _ in range(V) _ range(V) range V for _ in range(V) [] edge = [[] for _ in range(V)] edge [[] for _ in range(V)] for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v) _ range(V) range V u, _, *v = map(lambda x: int(x)-1, input().split()) u map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split _ *v v edge[u] = sorted(v) edge[u] edge u sorted(v) sorted v dist = [-1] * V dist [-1] * V [-1] -1 V dist[0] = 0 dist[0] dist 0 0 que = deque([0]) que deque([0]) deque [0] 0 while len(que):
    v = que.popleft()
    for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c) len(que) len que v = que.popleft() v que.popleft() que.popleft que popleft for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c) c edge[v] edge v if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c) dist[c] == -1 dist[c] dist c -1 dist[c] = dist[v] + 1 dist[c] dist c dist[v] + 1 dist[v] dist v 1 que.append(c) que.append que append c for i, d in enumerate(dist):
    print(i+1, d) i d enumerate(dist) enumerate dist print(i+1, d) print i+1 i 1 d dist = [-1] * V [-1] * V dist que = deque([0]) deque([0]) que edge = [[] for _ in range(V)] [[] for _ in range(V)] edge u, _, *v = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) u _, *v = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) _ v = que.popleft() que.popleft() v V = int(input()) int(input()) V
from collections import deque
V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v)
dist = [-1] * V
dist[0] = 0
que = deque([0])

while len(que):
    v = que.popleft()
    for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c)
for i, d in enumerate(dist):
    print(i+1, d)