from collections import deque
from enum import Enum

class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]?????????????????????


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = []
    for _ in range(num):
        data.append(list(map(int, input().split(' '))))

    # ???????????????
    # data[]????????????????????????1?????????????????£??\?????????????????????????´?????????????????????¨????????????????????????(??´???????????£??????????????????????????????)
    d = Bfs(data)
    d.search(1)

    # ???????????????
    for i in range(1, num+1):
        print('{0} {1}'.format(i, d.D[i])) from collections import deque from enum import Enum class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? Bfs """ Breadth First Search """ class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

     Status """ ?????????????¨??????¶??? """ white = 1 white 1 gray = 2 gray 2 black = 3 black 3 Enum def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

     __init__ num_of_nodes = len(data)+1 num_of_nodes len(data)+1 len(data) len data 1 self.color = [Bfs.Status.white] * (num_of_nodes + 1) self.color self color [Bfs.Status.white] * (num_of_nodes + 1) [Bfs.Status.white] Bfs.Status.white Bfs.Status Bfs Status white num_of_nodes + 1 num_of_nodes 1 self.Q = deque() self.Q self Q deque() deque self.M = self._make_matrix(data) self.M self M self._make_matrix(data) self._make_matrix self _make_matrix data self.D = [-1] * (len(data) + 1) self.D self D [-1] * (len(data) + 1) [-1] -1 len(data) + 1 len(data) len data 1 self self data data def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

     _make_matrix """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """ num = len(data) num len(data) len data [0] * (num+1) for _ in range(num+1) for _ in range(num+1) _ range(num+1) range num+1 num 1 for _ in range(num+1) [0] * (num+1) [0] 0 num+1 num 1 graph_table = [[0] * (num+1) for _ in range(num+1)] graph_table [[0] * (num+1) for _ in range(num+1)] for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
         d data node_id = d[0] node_id d[0] d 0 num_of_e = d[1] num_of_e d[1] d 1 for e in d[2:]:
                graph_table[node_id][e] = 1
         e d[2:] d 2: 2 graph_table[node_id][e] = 1 graph_table[node_id][e] [node_id] graph_table node_id e 1 return graph_table graph_table self self data data def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? search """ start????????????????????????????????°??????????????? """ self.Q.append(start) self.Q.append self.Q self Q append start self.color[start] = Bfs.Status.gray self.color[start] self.color self color start Bfs.Status.gray Bfs.Status Bfs Status gray self.D[start] = 0 self.D[start] self.D self D start 0 while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? self.Q self Q u = self.Q.popleft() u self.Q.popleft() self.Q.popleft self.Q self Q popleft edges = self.M[u] edges self.M[u] self.M self M u for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? i range(1, len(edges)) range 1 len(edges) len edges if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? edges[i] == 1 and self.color[i] == Bfs.Status.white edges[i] == 1 edges[i] edges i 1 self.color[i] == Bfs.Status.white self.color[i] self.color self color i Bfs.Status.white Bfs.Status Bfs Status white self.color[i] = Bfs.Status.gray self.color[i] self.color self color i Bfs.Status.gray Bfs.Status Bfs Status gray if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? self.D[i] == -1 self.D[i] self.D self D i -1 self.D[i] = self.D[u] + 1 self.D[i] self.D self D i self.D[u] + 1 self.D[u] self.D self D u 1 self.Q.append(i) self.Q.append self.Q self Q append i self self start start object if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = []
    for _ in range(num):
        data.append(list(map(int, input().split(' '))))

    # ???????????????
    # data[]????????????????????????1?????????????????£??\?????????????????????????´?????????????????????¨????????????????????????(??´???????????£??????????????????????????????)
    d = Bfs(data)
    d.search(1)

    # ???????????????
    for i in range(1, num+1):
        print('{0} {1}'.format(i, d.D[i])) __name__ == '__main__' __name__ '__main__' num = int(input()) num int(input()) int input() input data = [] data [] for _ in range(num):
        data.append(list(map(int, input().split(' '))))

    # ???????????????
    # data[]????????????????????????1?????????????????£??\?????????????????????????´?????????????????????¨????????????????????????(??´???????????£??????????????????????????????)
     _ range(num) range num data.append(list(map(int, input().split(' ')))) data.append data append list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' d = Bfs(data) d Bfs(data) Bfs data d.search(1) d.search d search 1 for i in range(1, num+1):
        print('{0} {1}'.format(i, d.D[i])) i range(1, num+1) range 1 num+1 num 1 print('{0} {1}'.format(i, d.D[i])) print '{0} {1}'.format(i, d.D[i]) '{0} {1}'.format '{0} {1}' format i d.D[i] d.D d D i num = int(input()) int(input()) num d = Bfs(data) Bfs(data) d data = [] [] data class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]????????????????????? Bfs
from collections import deque
from enum import Enum

class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]?????????????????????


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = []
    for _ in range(num):
        data.append(list(map(int, input().split(' '))))

    # ???????????????
    # data[]????????????????????????1?????????????????£??\?????????????????????????´?????????????????????¨????????????????????????(??´???????????£??????????????????????????????)
    d = Bfs(data)
    d.search(1)

    # ???????????????
    for i in range(1, num+1):
        print('{0} {1}'.format(i, d.D[i]))