from collections import deque


def get_next_node(node, discovery_time, adj_mat):
    for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
    return None

def bfs(node, adj_mat):
    distances = [-1] * len(adj_mat)

    distances[node] = 0
    q = deque(maxlen=len(adj_mat))
    q.append(0)
    while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
    for i, d in enumerate(distances):
        print(i+1, d)


if __name__ == "__main__":
    n_node = int(input())
    # f = open('alds1_11_c-input.txt', 'r')
    # n_node = int(f.readline())

    adjacency_mat = [[0 for i in range(n_node)] for j in range(n_node)]
    for i in range(n_node):
        command = list(map(int, input().split()))
        # command = list(map(int, f.readline().split()))

        i = command.pop(0) - 1
        n_edge = command.pop(0)

        for j in command:
            adjacency_mat[i][j-1] = 1

    # f.close()
    discovery_time = [0] * n_node
    finish_time = [0] * n_node
    bfs(0, adjacency_mat) from collections import deque def get_next_node(node, discovery_time, adj_mat):
    for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
    return None get_next_node for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
     i node enumerate(adj_mat[node]) enumerate adj_mat[node] adj_mat node if discovery_time[i] == 0 and node == 1:
            return i
     discovery_time[i] == 0 and node == 1 discovery_time[i] == 0 discovery_time[i] discovery_time i 0 node == 1 node 1 return i i return None None node node discovery_time discovery_time adj_mat adj_mat def bfs(node, adj_mat):
    distances = [-1] * len(adj_mat)

    distances[node] = 0
    q = deque(maxlen=len(adj_mat))
    q.append(0)
    while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
    for i, d in enumerate(distances):
        print(i+1, d) bfs distances = [-1] * len(adj_mat) distances [-1] * len(adj_mat) [-1] -1 len(adj_mat) len adj_mat distances[node] = 0 distances[node] distances node 0 q = deque(maxlen=len(adj_mat)) q deque(maxlen=len(adj_mat)) deque maxlen=len(adj_mat) maxlen len(adj_mat) len adj_mat q.append(0) q.append q append 0 while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
     q visit_node = q.popleft() visit_node q.popleft() q.popleft q popleft for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
     j range(len(adj_mat)) range len(adj_mat) len adj_mat if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
     adj_mat[visit_node][j] and distances[j]==-1 adj_mat[visit_node][j] [visit_node] adj_mat visit_node j distances[j]==-1 distances[j] distances j -1 distances[j] = distances[visit_node] + 1 distances[j] distances j distances[visit_node] + 1 distances[visit_node] distances visit_node 1 q.append(j) q.append q append j for i, d in enumerate(distances):
        print(i+1, d) i d enumerate(distances) enumerate distances print(i+1, d) print i+1 i 1 d node node adj_mat adj_mat if __name__ == "__main__":
    n_node = int(input())
    # f = open('alds1_11_c-input.txt', 'r')
    # n_node = int(f.readline())

    adjacency_mat = [[0 for i in range(n_node)] for j in range(n_node)]
    for i in range(n_node):
        command = list(map(int, input().split()))
        # command = list(map(int, f.readline().split()))

        i = command.pop(0) - 1
        n_edge = command.pop(0)

        for j in command:
            adjacency_mat[i][j-1] = 1

    # f.close()
    discovery_time = [0] * n_node
    finish_time = [0] * n_node
    bfs(0, adjacency_mat) __name__ == "__main__" __name__ "__main__" n_node = int(input()) n_node int(input()) int input() input [0 for i in range(n_node)] for j in range(n_node) for j in range(n_node) j range(n_node) range n_node for j in range(n_node) [0 for i in range(n_node)] adjacency_mat = [[0 for i in range(n_node)] for j in range(n_node)] adjacency_mat [[0 for i in range(n_node)] for j in range(n_node)] for i in range(n_node):
        command = list(map(int, input().split()))
        # command = list(map(int, f.readline().split()))

        i = command.pop(0) - 1
        n_edge = command.pop(0)

        for j in command:
            adjacency_mat[i][j-1] = 1

    # f.close()
     i range(n_node) range n_node command = list(map(int, input().split())) command list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i = command.pop(0) - 1 i command.pop(0) - 1 command.pop(0) command.pop command pop 0 1 n_edge = command.pop(0) n_edge command.pop(0) command.pop command pop 0 for j in command:
            adjacency_mat[i][j-1] = 1

    # f.close()
     j command adjacency_mat[i][j-1] = 1 adjacency_mat[i][j-1] [i] adjacency_mat i j-1 j 1 1 discovery_time = [0] * n_node discovery_time [0] * n_node [0] 0 n_node finish_time = [0] * n_node finish_time [0] * n_node [0] 0 n_node bfs(0, adjacency_mat) bfs 0 adjacency_mat command = list(map(int, input().split())) list(map(int, input().split())) command n_edge = command.pop(0) command.pop(0) n_edge def bfs(node, adj_mat):
    distances = [-1] * len(adj_mat)

    distances[node] = 0
    q = deque(maxlen=len(adj_mat))
    q.append(0)
    while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
    for i, d in enumerate(distances):
        print(i+1, d) def bfs(node, adj_mat):
    distances = [-1] * len(adj_mat)

    distances[node] = 0
    q = deque(maxlen=len(adj_mat))
    q.append(0)
    while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
    for i, d in enumerate(distances):
        print(i+1, d) bfs discovery_time = [0] * n_node [0] * n_node discovery_time n_node = int(input()) int(input()) n_node adjacency_mat = [[0 for i in range(n_node)] for j in range(n_node)] [[0 for i in range(n_node)] for j in range(n_node)] adjacency_mat finish_time = [0] * n_node [0] * n_node finish_time i = command.pop(0) - 1 command.pop(0) - 1 i def get_next_node(node, discovery_time, adj_mat):
    for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
    return None def get_next_node(node, discovery_time, adj_mat):
    for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
    return None get_next_node
from collections import deque


def get_next_node(node, discovery_time, adj_mat):
    for i, node in enumerate(adj_mat[node]):
        if discovery_time[i] == 0 and node == 1:
            return i
    return None

def bfs(node, adj_mat):
    distances = [-1] * len(adj_mat)

    distances[node] = 0
    q = deque(maxlen=len(adj_mat))
    q.append(0)
    while q:
        visit_node = q.popleft()
        for j in range(len(adj_mat)):
            if adj_mat[visit_node][j] and distances[j]==-1:
                distances[j] = distances[visit_node] + 1
                q.append(j)
    
    for i, d in enumerate(distances):
        print(i+1, d)


if __name__ == "__main__":
    n_node = int(input())
    # f = open('alds1_11_c-input.txt', 'r')
    # n_node = int(f.readline())

    adjacency_mat = [[0 for i in range(n_node)] for j in range(n_node)]
    for i in range(n_node):
        command = list(map(int, input().split()))
        # command = list(map(int, f.readline().split()))

        i = command.pop(0) - 1
        n_edge = command.pop(0)

        for j in command:
            adjacency_mat[i][j-1] = 1

    # f.close()
    discovery_time = [0] * n_node
    finish_time = [0] * n_node
    bfs(0, adjacency_mat)
