ii = lambda : int(input())
li = lambda : list(map(int,input().split()))

from collections import deque

class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:]

n = ii()
t = tree(n)
for i in range(n):
    uk = li()
    t.add_arr(uk[0],uk[1:])
l = deque()
l.append(0)
t.root[0] = 0
while len(l)>0:
    p = l.popleft()
    for i in t.arr[p]:
        if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1)

for k,i in enumerate(t.root):
    print(k+1,i) ii = lambda : int(input()) ii lambda : int(input()) int(input()) int input() input li = lambda : list(map(int,input().split())) li lambda : list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split from collections import deque class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:] tree def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

     __init__ self.n = n self.n self n n [] for i in range(self.n) for i in range(self.n) i range(self.n) range self.n self n for i in range(self.n) [] self.arr = [[] for i in range(self.n)] self.arr self arr [[] for i in range(self.n)] self.root = [-1]*n self.root self root [-1]*n [-1] -1 n self self n n def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:] add_arr if v[0] != 0:
            self.arr[u-1] = v[1:] v[0] != 0 v[0] v 0 0 self.arr[u-1] = v[1:] self.arr[u-1] self.arr self arr u-1 u 1 v[1:] v 1: 1 self self u u v v n = ii() n ii() ii t = tree(n) t tree(n) tree n for i in range(n):
    uk = li()
    t.add_arr(uk[0],uk[1:]) i range(n) range n uk = li() uk li() li t.add_arr(uk[0],uk[1:]) t.add_arr t add_arr uk[0] uk 0 uk[1:] uk 1: 1 l = deque() l deque() deque l.append(0) l.append l append 0 t.root[0] = 0 t.root[0] t.root t root 0 0 while len(l)>0:
    p = l.popleft()
    for i in t.arr[p]:
        if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1) len(l)>0 len(l) len l 0 p = l.popleft() p l.popleft() l.popleft l popleft for i in t.arr[p]:
        if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1) i t.arr[p] t.arr t arr p if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1) t.root[i-1] == -1 t.root[i-1] t.root t root i-1 i 1 -1 t.root[i-1] = t.root[p]+1 t.root[i-1] t.root t root i-1 i 1 t.root[p]+1 t.root[p] t.root t root p 1 l.append(i-1) l.append l append i-1 i 1 for k,i in enumerate(t.root):
    print(k+1,i) k i enumerate(t.root) enumerate t.root t root print(k+1,i) print k+1 k 1 i li = lambda : list(map(int,input().split())) lambda : list(map(int,input().split())) li n = ii() ii() n class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:] class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:] tree l = deque() deque() l t = tree(n) tree(n) t p = l.popleft() l.popleft() p uk = li() li() uk ii = lambda : int(input()) lambda : int(input()) ii
ii = lambda : int(input())
li = lambda : list(map(int,input().split()))

from collections import deque

class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:]

n = ii()
t = tree(n)
for i in range(n):
    uk = li()
    t.add_arr(uk[0],uk[1:])
l = deque()
l.append(0)
t.root[0] = 0
while len(l)>0:
    p = l.popleft()
    for i in t.arr[p]:
        if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1)

for k,i in enumerate(t.root):
    print(k+1,i)
