class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x


class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

n = int(input())
queue = Myqueue(n)
nodes = [Node() for _ in range(n)]
[node.input_data(nodes) for node in nodes]
nodes[0].status = 1
nodes[0].distance = 0
queue.enqueue(nodes[0])
while not queue.is_empty():
    u = queue.dequeue()
    for v in u.v: #type: Node
        if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
    u.status = 2


nodes.sort(key=lambda i: i.number)
for node in nodes:
    print(f"{node.number} {node.distance}") class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x Myqueue def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

     __init__ self.size = size self.size self size size None for _ in range(size) for _ in range(size) _ range(size) range size for _ in range(size) None self.N = [None for _ in range(size)] self.N self N [None for _ in range(size)] self.head = 0 self.head self head 0 self.tail = 0 self.tail self tail 0 self self size size def is_empty(self):
        return self.head == self.tail

     is_empty return self.head == self.tail self.head == self.tail self.head self head self.tail self tail self self def is_full(self):
        return self.head == (self.tail + 1) % self.size

     is_full return self.head == (self.tail + 1) % self.size self.head == (self.tail + 1) % self.size self.head self head (self.tail + 1) % self.size self.tail + 1 self.tail self tail 1 self.size self size self self def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

     enqueue if self.is_full():
            raise OverflowError
         self.is_full() self.is_full self is_full raise OverflowError OverflowError self.N[self.tail] = x self.N[self.tail] self.N self N self.tail self tail x self.tail += 1 self.tail self tail 1 if self.tail == self.size:
            self.tail = 0

     self.tail == self.size self.tail self tail self.size self size self.tail = 0 self.tail self tail 0 self self x x def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x dequeue if self.is_empty():
            raise IndexError
         self.is_empty() self.is_empty self is_empty raise IndexError IndexError x = self.N[self.head] x self.N[self.head] self.N self N self.head self head self.head += 1 self.head self head 1 if self.head == self.size:
            self.head = 0
         self.head == self.size self.head self head self.size self size self.head = 0 self.head self head 0 return x x self self class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]] Node def __init__(self):
        self.distance = -1
        self.status = 0

     __init__ self.distance = -1 self.distance self distance -1 self.status = 0 self.status self status 0 self self def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]] input_data data = input().split() data input().split() ().split () input split self.number = int(data[0]) self.number self number int(data[0]) int data[0] data 0 nodes[int(x) - 1] for x in data[2:] for x in data[2:] x data[2:] data 2: 2 for x in data[2:] nodes[int(x) - 1] nodes int(x) - 1 int(x) int x 1 self.v = [nodes[int(x) - 1] for x in data[2:]] self.v self v [nodes[int(x) - 1] for x in data[2:]] self self nodes nodes n = int(input()) n int(input()) int input() input queue = Myqueue(n) queue Myqueue(n) Myqueue n Node() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) Node() Node nodes = [Node() for _ in range(n)] nodes [Node() for _ in range(n)] node.input_data(nodes) for node in nodes for node in nodes node nodes for node in nodes node.input_data(nodes) node.input_data node input_data nodes [node.input_data(nodes) for node in nodes] nodes[0].status = 1 nodes[0].status [0] nodes 0 status 1 nodes[0].distance = 0 nodes[0].distance [0] nodes 0 distance 0 queue.enqueue(nodes[0]) queue.enqueue queue enqueue nodes[0] nodes 0 while not queue.is_empty():
    u = queue.dequeue()
    for v in u.v: #type: Node
        if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
    u.status = 2 not queue.is_empty() queue.is_empty() queue.is_empty queue is_empty u = queue.dequeue() u queue.dequeue() queue.dequeue queue dequeue for v in u.v: #type: Node
        if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
     v u.v u v if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
     v.status == 0 v.status v status 0 v.status = 1 v.status v status 1 v.distance = u.distance + 1 v.distance v distance u.distance + 1 u.distance u distance 1 queue.enqueue(v) queue.enqueue queue enqueue v u.status = 2 u.status u status 2 nodes.sort(key=lambda i: i.number) nodes.sort nodes sort key=lambda i: i.number key lambda i: i.number i.number i number i for node in nodes:
    print(f"{node.number} {node.distance}") node nodes print(f"{node.number} {node.distance}") print f"{node.number} {node.distance}" queue = Myqueue(n) Myqueue(n) queue nodes = [Node() for _ in range(n)] [Node() for _ in range(n)] nodes class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x Myqueue u = queue.dequeue() queue.dequeue() u class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]] class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]] Node n = int(input()) int(input()) n
class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x


class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

n = int(input())
queue = Myqueue(n)
nodes = [Node() for _ in range(n)]
[node.input_data(nodes) for node in nodes]
nodes[0].status = 1
nodes[0].distance = 0
queue.enqueue(nodes[0])
while not queue.is_empty():
    u = queue.dequeue()
    for v in u.v: #type: Node
        if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
    u.status = 2


nodes.sort(key=lambda i: i.number)
for node in nodes:
    print(f"{node.number} {node.distance}")
