from collections import deque


def breadth_first_search(adj_matrix, n):
    distance = [0] + [-1] * (n - 1)
    searching = deque([0])
    while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
    return distance


def main():
    n = int(input())
    adj = [[False for _ in range(n)] for _ in range(n)]
    for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
    distance = breadth_first_search(adj, n)
    for i in range(n):
        print("{} {}".format(i + 1, distance[i]))


if __name__ == "__main__":
    main()
 from collections import deque def breadth_first_search(adj_matrix, n):
    distance = [0] + [-1] * (n - 1)
    searching = deque([0])
    while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
    return distance breadth_first_search distance = [0] + [-1] * (n - 1) distance [0] + [-1] * (n - 1) [0] 0 [-1] * (n - 1) [-1] -1 n - 1 n 1 searching = deque([0]) searching deque([0]) deque [0] 0 while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
     len(searching) > 0 len(searching) len searching 0 start = searching.popleft() start searching.popleft() searching.popleft searching popleft for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
     end range(n) range n if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
     adj_matrix[start][end] and distance[end] == -1 adj_matrix[start][end] [start] adj_matrix start end distance[end] == -1 distance[end] distance end -1 searching.append(end) searching.append searching append end distance[end] = distance[start] + 1 distance[end] distance end distance[start] + 1 distance[start] distance start 1 return distance distance adj_matrix adj_matrix n n def main():
    n = int(input())
    adj = [[False for _ in range(n)] for _ in range(n)]
    for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
    distance = breadth_first_search(adj, n)
    for i in range(n):
        print("{} {}".format(i + 1, distance[i])) main n = int(input()) n int(input()) int input() input [False for _ in range(n)] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [False for _ in range(n)] adj = [[False for _ in range(n)] for _ in range(n)] adj [[False for _ in range(n)] for _ in range(n)] for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
     _ range(n) range n ukv = input().split() ukv input().split() ().split () input split id = int(ukv[0]) id int(ukv[0]) int ukv[0] ukv 0 dim = int(ukv[1]) dim int(ukv[1]) int ukv[1] ukv 1 if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
     dim > 0 dim 0 nodes = map(int, ukv[2:]) nodes map(int, ukv[2:]) map int ukv[2:] ukv 2: 2 for node in nodes:
                adj[id - 1][node - 1] = True
     node nodes adj[id - 1][node - 1] = True adj[id - 1][node - 1] [id - 1] adj id - 1 id 1 node - 1 node 1 True distance = breadth_first_search(adj, n) distance breadth_first_search(adj, n) breadth_first_search adj n for i in range(n):
        print("{} {}".format(i + 1, distance[i])) i range(n) range n print("{} {}".format(i + 1, distance[i])) print "{} {}".format(i + 1, distance[i]) "{} {}".format "{} {}" format i + 1 i 1 distance[i] distance i if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def breadth_first_search(adj_matrix, n):
    distance = [0] + [-1] * (n - 1)
    searching = deque([0])
    while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
    return distance def breadth_first_search(adj_matrix, n):
    distance = [0] + [-1] * (n - 1)
    searching = deque([0])
    while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
    return distance breadth_first_search def main():
    n = int(input())
    adj = [[False for _ in range(n)] for _ in range(n)]
    for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
    distance = breadth_first_search(adj, n)
    for i in range(n):
        print("{} {}".format(i + 1, distance[i])) def main():
    n = int(input())
    adj = [[False for _ in range(n)] for _ in range(n)]
    for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
    distance = breadth_first_search(adj, n)
    for i in range(n):
        print("{} {}".format(i + 1, distance[i])) main
from collections import deque


def breadth_first_search(adj_matrix, n):
    distance = [0] + [-1] * (n - 1)
    searching = deque([0])
    while len(searching) > 0:
        start = searching.popleft()
        for end in range(n):
            if adj_matrix[start][end] and distance[end] == -1:
                searching.append(end)
                distance[end] = distance[start] + 1
    return distance


def main():
    n = int(input())
    adj = [[False for _ in range(n)] for _ in range(n)]
    for _ in range(n):
        ukv = input().split()
        id = int(ukv[0])
        dim = int(ukv[1])
        if dim > 0:
            nodes = map(int, ukv[2:])
            for node in nodes:
                adj[id - 1][node - 1] = True
    distance = breadth_first_search(adj, n)
    for i in range(n):
        print("{} {}".format(i + 1, distance[i]))


if __name__ == "__main__":
    main()

