from collections import deque

n = int(input())
u = [[] for i in range(n+1)] #隣接リスト
for i in range(n):
    v = list(map(int, input().split()))
    u[v[0]] = v[1:] #v = [次数, 頂点...]

d = [-1] * (n+1)
visited = [False] * (n+1)

d[1] = 0
visited[1] = True
que = deque([1])
while len(que) > 0:
    c = que.popleft()
    for i in range(1, u[c][0]+1):
        if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1

for i in range(1, n+1):
    print(i, d[i]) from collections import deque n = int(input()) n int(input()) int input() input [] for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [] u = [[] for i in range(n+1)] u [[] for i in range(n+1)] for i in range(n):
    v = list(map(int, input().split()))
    u[v[0]] = v[1:] #v = [次数, 頂点...] i range(n) range n v = list(map(int, input().split())) v list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u[v[0]] = v[1:] u[v[0]] u v[0] v 0 v[1:] v 1: 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 que = deque([1]) que deque([1]) deque [1] 1 while len(que) > 0:
    c = que.popleft()
    for i in range(1, u[c][0]+1):
        if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1 len(que) > 0 len(que) len que 0 c = que.popleft() c que.popleft() que.popleft que popleft for i in range(1, u[c][0]+1):
        if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1 i range(1, u[c][0]+1) range 1 u[c][0]+1 u[c][0] [c] u c 0 1 if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1 not visited[u[c][i]] visited[u[c][i]] visited u[c][i] [c] u c i que.append(u[c][i]) que.append que append u[c][i] [c] u c i visited[u[c][i]] = True visited[u[c][i]] visited u[c][i] [c] u c i True d[u[c][i]] = d[c] + 1 d[u[c][i]] d u[c][i] [c] u c i d[c] + 1 d[c] d c 1 for i in range(1, n+1):
    print(i, d[i]) i range(1, n+1) range 1 n+1 n 1 print(i, d[i]) print i d[i] d i u = [[] for i in range(n+1)] [[] for i in range(n+1)] u d = [-1] * (n+1) [-1] * (n+1) d visited = [False] * (n+1) [False] * (n+1) visited que = deque([1]) deque([1]) que v = list(map(int, input().split())) list(map(int, input().split())) v n = int(input()) int(input()) n c = que.popleft() que.popleft() c
from collections import deque

n = int(input())
u = [[] for i in range(n+1)] #隣接リスト
for i in range(n):
    v = list(map(int, input().split()))
    u[v[0]] = v[1:] #v = [次数, 頂点...]

d = [-1] * (n+1)
visited = [False] * (n+1)

d[1] = 0
visited[1] = True
que = deque([1])
while len(que) > 0:
    c = que.popleft()
    for i in range(1, u[c][0]+1):
        if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1

for i in range(1, n+1):
    print(i, d[i])
