from collections import deque

n = int(input())
g = []
# ans = [[-1]*n for _ in range(n)]
ans = [-1] * n
done = set()
q = deque()

for i in range(n):
    temp = list(map(int, input().split()))
    g.append(temp[2:])

# print(g)

q.append(0)
# dis = 1
ans[0] = 0
done.add(1)

while q:
    idx = q.popleft()
    adj = g[idx]
    # print('node:', idx+1)

    for i in adj:
        if i not in done:
            ans[i-1] = ans[idx] + 1
            done.add(i)
            q.append(i-1)
    # if len(adj) > 0:
    #     dis += 1

# print(ans)

for i in range(n):
    print(i+1, ans[i]) from collections import deque n = int(input()) n int(input()) int input() input g = [] g [] ans = [-1] * n ans [-1] * n [-1] -1 n done = set() done set() set q = deque() q deque() deque for i in range(n):
    temp = list(map(int, input().split()))
    g.append(temp[2:])

# print(g) i range(n) range n temp = list(map(int, input().split())) temp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split g.append(temp[2:]) g.append g append temp[2:] temp 2: 2 q.append(0) q.append q append 0 ans[0] = 0 ans[0] ans 0 0 done.add(1) done.add done add 1 while q:
    idx = q.popleft()
    adj = g[idx]
    # print('node:', idx+1)

    for i in adj:
        if i not in done:
            ans[i-1] = ans[idx] + 1
            done.add(i)
            q.append(i-1)
    # if len(adj) > 0:
    #     dis += 1

# print(ans) q idx = q.popleft() idx q.popleft() q.popleft q popleft adj = g[idx] adj g[idx] g idx for i in adj:
        if i not in done:
            ans[i-1] = ans[idx] + 1
            done.add(i)
            q.append(i-1)
    # if len(adj) > 0:
    #     dis += 1

# print(ans) i adj if i not in done:
            ans[i-1] = ans[idx] + 1
            done.add(i)
            q.append(i-1)
    # if len(adj) > 0:
    #     dis += 1

# print(ans) i not in done i done ans[i-1] = ans[idx] + 1 ans[i-1] ans i-1 i 1 ans[idx] + 1 ans[idx] ans idx 1 done.add(i) done.add done add i q.append(i-1) q.append q append i-1 i 1 for i in range(n):
    print(i+1, ans[i]) i range(n) range n print(i+1, ans[i]) print i+1 i 1 ans[i] ans i ans = [-1] * n [-1] * n ans g = [] [] g done = set() set() done idx = q.popleft() q.popleft() idx n = int(input()) int(input()) n adj = g[idx] g[idx] adj temp = list(map(int, input().split())) list(map(int, input().split())) temp q = deque() deque() q
from collections import deque

n = int(input())
g = []
# ans = [[-1]*n for _ in range(n)]
ans = [-1] * n
done = set()
q = deque()

for i in range(n):
    temp = list(map(int, input().split()))
    g.append(temp[2:])

# print(g)

q.append(0)
# dis = 1
ans[0] = 0
done.add(1)

while q:
    idx = q.popleft()
    adj = g[idx]
    # print('node:', idx+1)

    for i in adj:
        if i not in done:
            ans[i-1] = ans[idx] + 1
            done.add(i)
            q.append(i-1)
    # if len(adj) > 0:
    #     dis += 1

# print(ans)

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