from collections import deque
n = int(input())
u = []
k = []
v = []

for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
        
d = deque([1])
dis = [-1]*n
dis[0] = 0
visit = [False]*n
visit[0] = True
while len(d) > 0:
    go = d[0]
    flag = False
    for i in v[go-1]:
        if not visit[i-1]:
            d.append(i)
            dis[i-1] = dis[go-1] + 1
            visit[i-1] = True
            flag = True
    d.popleft()
    
for i in range(n):
    print(i+1, dis[i]) from collections import deque n = int(input()) n int(input()) int input() input u = [] u [] k = [] k [] v = [] v [] for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
         i range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u.append(tmp[0]) u.append u append tmp[0] tmp 0 k.append(tmp[1]) k.append k append tmp[1] tmp 1 if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
         tmp[1] != 0 tmp[1] tmp 1 0 v.append(tmp[2:]) v.append v append tmp[2:] tmp 2: 2 v.append([]) v.append v append [] d = deque([1]) d deque([1]) deque [1] 1 dis = [-1]*n dis [-1]*n [-1] -1 n dis[0] = 0 dis[0] dis 0 0 visit = [False]*n visit [False]*n [False] False n visit[0] = True visit[0] visit 0 True while len(d) > 0:
    go = d[0]
    flag = False
    for i in v[go-1]:
        if not visit[i-1]:
            d.append(i)
            dis[i-1] = dis[go-1] + 1
            visit[i-1] = True
            flag = True
    d.popleft()
     len(d) > 0 len(d) len d 0 go = d[0] go d[0] d 0 flag = False flag False for i in v[go-1]:
        if not visit[i-1]:
            d.append(i)
            dis[i-1] = dis[go-1] + 1
            visit[i-1] = True
            flag = True
     i v[go-1] v go-1 go 1 if not visit[i-1]:
            d.append(i)
            dis[i-1] = dis[go-1] + 1
            visit[i-1] = True
            flag = True
     not visit[i-1] visit[i-1] visit i-1 i 1 d.append(i) d.append d append i dis[i-1] = dis[go-1] + 1 dis[i-1] dis i-1 i 1 dis[go-1] + 1 dis[go-1] dis go-1 go 1 1 visit[i-1] = True visit[i-1] visit i-1 i 1 True flag = True flag True d.popleft() d.popleft d popleft for i in range(n):
    print(i+1, dis[i]) i range(n) range n print(i+1, dis[i]) print i+1 i 1 dis[i] dis i v = [] [] v d = deque([1]) deque([1]) d tmp = list(map(int, input().split())) list(map(int, input().split())) tmp n = int(input()) int(input()) n flag = True True flag flag = False False flag dis = [-1]*n [-1]*n dis visit = [False]*n [False]*n visit go = d[0] d[0] go k = [] [] k u = [] [] u
from collections import deque
n = int(input())
u = []
k = []
v = []

for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
        
d = deque([1])
dis = [-1]*n
dis[0] = 0
visit = [False]*n
visit[0] = True
while len(d) > 0:
    go = d[0]
    flag = False
    for i in v[go-1]:
        if not visit[i-1]:
            d.append(i)
            dis[i-1] = dis[go-1] + 1
            visit[i-1] = True
            flag = True
    d.popleft()
    
for i in range(n):
    print(i+1, dis[i])
