n = int(input())

class Node:
    def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
    def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
    
def breadthFirstSearch(Nodes):
    stack = [Nodes[0]]
    Nodes[0].dist = 0
    while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
    return Nodes

Nodes = [None] * n
for i in range(n):
    row = [int(tt) for tt in input().split()]
    Nodes[row[0]-1] = Node(row[0], [ row[j + 2] for j in range(row[1])])

Nodes = breadthFirstSearch(Nodes)
for node in Nodes:
    print(node) n = int(input()) n int(input()) int input() input class Node:
    def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
    def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
     Node def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
     __init__ self.idd = idd self.idd self idd idd self.neighbors = neighbors self.neighbors self neighbors neighbors self.dist = -1 self.dist self dist -1 self self idd idd neighbors neighbors def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
     __repr__ return "{0} {1}".format(self.idd, self.dist) "{0} {1}".format(self.idd, self.dist) "{0} {1}".format "{0} {1}" format self.idd self idd self.dist self dist self self def breadthFirstSearch(Nodes):
    stack = [Nodes[0]]
    Nodes[0].dist = 0
    while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
    return Nodes breadthFirstSearch stack = [Nodes[0]] stack [Nodes[0]] Nodes[0] Nodes 0 Nodes[0].dist = 0 Nodes[0].dist [0] Nodes 0 dist 0 while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
     len(stack) > 0 len(stack) len stack 0 current = stack.pop(0) current stack.pop(0) stack.pop stack pop 0 for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
     v current.neighbors current neighbors v_node = Nodes[v-1] v_node Nodes[v-1] Nodes v-1 v 1 if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
     v_node.dist == -1 v_node.dist v_node dist -1 v_node.dist = current.dist + 1 v_node.dist v_node dist current.dist + 1 current.dist current dist 1 stack.append(v_node) stack.append stack append v_node v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1 v_node.dist v_node dist v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1 v_node.dist < current.dist + 1 v_node.dist v_node dist current.dist + 1 current.dist current dist 1 v_node.dist v_node dist current.dist + 1 current.dist current dist 1 return Nodes Nodes Nodes Nodes Nodes = [None] * n Nodes [None] * n [None] None n for i in range(n):
    row = [int(tt) for tt in input().split()]
    Nodes[row[0]-1] = Node(row[0], [ row[j + 2] for j in range(row[1])]) i range(n) range n int(tt) for tt in input().split() for tt in input().split() tt input().split() ().split () input split for tt in input().split() int(tt) int tt row = [int(tt) for tt in input().split()] row [int(tt) for tt in input().split()] row[j + 2] for j in range(row[1]) for j in range(row[1]) j range(row[1]) range row[1] row 1 for j in range(row[1]) row[j + 2] row j + 2 j 2 Nodes[row[0]-1] = Node(row[0], [ row[j + 2] for j in range(row[1])]) Nodes[row[0]-1] Nodes row[0]-1 row[0] row 0 1 Node(row[0], [ row[j + 2] for j in range(row[1])]) Node row[0] row 0 [ row[j + 2] for j in range(row[1])] Nodes = breadthFirstSearch(Nodes) Nodes breadthFirstSearch(Nodes) breadthFirstSearch Nodes for node in Nodes:
    print(node) node Nodes print(node) print node Nodes = breadthFirstSearch(Nodes) breadthFirstSearch(Nodes) Nodes n = int(input()) int(input()) n class Node:
    def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
    def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
     class Node:
    def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
    def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
     Node row = [int(tt) for tt in input().split()] [int(tt) for tt in input().split()] row def breadthFirstSearch(Nodes):
    stack = [Nodes[0]]
    Nodes[0].dist = 0
    while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
    return Nodes def breadthFirstSearch(Nodes):
    stack = [Nodes[0]]
    Nodes[0].dist = 0
    while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
    return Nodes breadthFirstSearch Nodes = [None] * n [None] * n Nodes
n = int(input())

class Node:
    def __init__(self, idd, neighbors):
        self.idd = idd
        self.neighbors = neighbors
        self.dist = -1
        
    def __repr__(self):
        return "{0} {1}".format(self.idd, self.dist)
    
def breadthFirstSearch(Nodes):
    stack = [Nodes[0]]
    Nodes[0].dist = 0
    while len(stack) > 0:
        current = stack.pop(0)
        for v in current.neighbors:
            v_node = Nodes[v-1]
#             print('current:{}, v_node:{}'.format(current, v_node))
            if(v_node.dist == -1):
                v_node.dist = current.dist + 1
                stack.append(v_node)
            else:
                v_node.dist = v_node.dist if v_node.dist < current.dist + 1 else current.dist + 1
    return Nodes

Nodes = [None] * n
for i in range(n):
    row = [int(tt) for tt in input().split()]
    Nodes[row[0]-1] = Node(row[0], [ row[j + 2] for j in range(row[1])])

Nodes = breadthFirstSearch(Nodes)
for node in Nodes:
    print(node)
