from collections import deque

#キューを使った幅優先探索の実装
l = int(input())
rlist = []
for i in range(l):
    li = list(map(int, input().split()))
    rlist.append(deque([ll-1 for ll in li[2:]]))


color = ['white'] * l
queue = deque([])
d = [10000000] * l

def bfs(u):
    color[u] = 'gray'
    d[u] = 0
    queue.append(u)

    while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black'

bfs(0)

for i in range(l):
    if d[i] == 10000000:
        d[i] = -1

for i in range(l):
    print(str(i+1) + ' ' + str(d[i])) from collections import deque l = int(input()) l int(input()) int input() input rlist = [] rlist [] for i in range(l):
    li = list(map(int, input().split()))
    rlist.append(deque([ll-1 for ll in li[2:]])) i range(l) range l li = list(map(int, input().split())) li list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ll-1 for ll in li[2:] for ll in li[2:] ll li[2:] li 2: 2 for ll in li[2:] ll-1 ll 1 rlist.append(deque([ll-1 for ll in li[2:]])) rlist.append rlist append deque([ll-1 for ll in li[2:]]) deque [ll-1 for ll in li[2:]] color = ['white'] * l color ['white'] * l ['white'] 'white' l queue = deque([]) queue deque([]) deque [] d = [10000000] * l d [10000000] * l [10000000] 10000000 l def bfs(u):
    color[u] = 'gray'
    d[u] = 0
    queue.append(u)

    while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black' bfs color[u] = 'gray' color[u] color u 'gray' d[u] = 0 d[u] d u 0 queue.append(u) queue.append queue append u while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black' len(queue) > 0 len(queue) len queue 0 u = queue.popleft() u queue.popleft() queue.popleft queue popleft for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
         rl rlist[u] rlist u if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
         color[rl] == 'white' color[rl] color rl 'white' color[rl] = 'gray' color[rl] color rl 'gray' d[rl] = d[u] + 1 d[rl] d rl d[u] + 1 d[u] d u 1 queue.append(rl) queue.append queue append rl color[u] = 'black' color[u] color u 'black' u u bfs(0) bfs 0 for i in range(l):
    if d[i] == 10000000:
        d[i] = -1 i range(l) range l if d[i] == 10000000:
        d[i] = -1 d[i] == 10000000 d[i] d i 10000000 d[i] = -1 d[i] d i -1 for i in range(l):
    print(str(i+1) + ' ' + str(d[i])) i range(l) range l print(str(i+1) + ' ' + str(d[i])) print str(i+1) + ' ' + str(d[i]) str(i+1) + ' ' str(i+1) str i+1 i 1 ' ' str(d[i]) str d[i] d i li = list(map(int, input().split())) list(map(int, input().split())) li l = int(input()) int(input()) l def bfs(u):
    color[u] = 'gray'
    d[u] = 0
    queue.append(u)

    while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black' def bfs(u):
    color[u] = 'gray'
    d[u] = 0
    queue.append(u)

    while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black' bfs rlist = [] [] rlist d = [10000000] * l [10000000] * l d color = ['white'] * l ['white'] * l color queue = deque([]) deque([]) queue
from collections import deque

#キューを使った幅優先探索の実装
l = int(input())
rlist = []
for i in range(l):
    li = list(map(int, input().split()))
    rlist.append(deque([ll-1 for ll in li[2:]]))


color = ['white'] * l
queue = deque([])
d = [10000000] * l

def bfs(u):
    color[u] = 'gray'
    d[u] = 0
    queue.append(u)

    while(len(queue) > 0):
        u = queue.popleft()
        for rl in rlist[u]:
            if color[rl] == 'white':
                color[rl] = 'gray'
                d[rl] = d[u] + 1
                queue.append(rl)
        color[u] = 'black'

bfs(0)

for i in range(l):
    if d[i] == 10000000:
        d[i] = -1

for i in range(l):
    print(str(i+1) + ' ' + str(d[i]))
