# coding: utf-8

from queue import Queue

N = int(input())
graph = [None]
for _ in range(N):
    graph.append(list(map(int, input().split(" ")))[1:])

class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i))

s = BFSSolver(graph)
s.solve()
 from queue import Queue N = int(input()) N int(input()) int input() input graph = [None] graph [None] None for _ in range(N):
    graph.append(list(map(int, input().split(" ")))[1:]) _ range(N) range N graph.append(list(map(int, input().split(" ")))[1:]) graph.append graph append list(map(int, input().split(" ")))[1:] (map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " 1: 1 class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i)) BFSSolver def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

     __init__ self.graph = graph self.graph self graph graph self.queue = Queue() self.queue self queue Queue() Queue self.queue.put(1) self.queue.put self.queue self queue put 1 self.before_id = 1 self.before_id self before_id 1 self.already_searched = [1] self.already_searched self already_searched [1] 1 self.res = [0] self.res self res [0] 0 for _ in range(N):
            self.res.append(-1)
         _ range(N) range N self.res.append(-1) self.res.append self.res self res append -1 self.res[1] = 0 self.res[1] self.res self res 1 0 self.turn = 0 self.turn self turn 0 self self graph graph def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

     next_turn now_id = self.queue.get() now_id self.queue.get() self.queue.get self.queue self queue get before_id = now_id before_id now_id neighbors = self.graph[now_id][1:] neighbors self.graph[now_id][1:] self.graph[now_id] self.graph self graph now_id 1: 1 for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

     i neighbors if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

     i not in self.already_searched i self.already_searched self already_searched self.turn = self.res[before_id] + 1 self.turn self turn self.res[before_id] + 1 self.res[before_id] self.res self res before_id 1 self.already_searched.append(i) self.already_searched.append self.already_searched self already_searched append i self.res[i] = self.turn self.res[i] self.res self res i self.turn self turn self.queue.put(i) self.queue.put self.queue self queue put i self self def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i)) solve while not self.queue.empty():
            self.next_turn()

         not self.queue.empty() self.queue.empty() self.queue.empty self.queue self queue empty self.next_turn() self.next_turn self next_turn for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i)) id i enumerate(self.res[1:]) enumerate self.res[1:] self.res self res 1: 1 print("{} {}".format(id+1, i)) print "{} {}".format(id+1, i) "{} {}".format "{} {}" format id+1 id 1 i self self s = BFSSolver(graph) s BFSSolver(graph) BFSSolver graph s.solve() s.solve s solve s = BFSSolver(graph) BFSSolver(graph) s class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i)) class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i)) BFSSolver graph = [None] [None] graph N = int(input()) int(input()) N
# coding: utf-8

from queue import Queue

N = int(input())
graph = [None]
for _ in range(N):
    graph.append(list(map(int, input().split(" ")))[1:])

class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i))

s = BFSSolver(graph)
s.solve()

