def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    d = bfs(G, 1)
    for node in G:
        print('{0} {1}'.format(node, d[node])) def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d bfs key: -1 for key in G.keys() for key in G.keys() key G.keys() G.keys G keys for key in G.keys() key -1 d = {key: -1 for key in G.keys()} d {key: -1 for key in G.keys()} d[s] = 0 d[s] d s 0 Q = [s] Q [s] s while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

     len(Q) > 0 len(Q) len Q 0 u = Q.pop(0) u Q.pop(0) Q.pop Q pop 0 for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

     v G[u] G u if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

     d[v] < 0 d[v] d v 0 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.append(v) Q.append Q append v return d d G G s s if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    d = bfs(G, 1)
    for node in G:
        print('{0} {1}'.format(node, d[node])) __name__ == '__main__' __name__ '__main__' G = {} G {} n = int(input()) n int(input()) int input() input for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
     _ range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = tmp.pop(0) u tmp.pop(0) tmp.pop tmp pop 0 k = tmp.pop(0) k tmp.pop(0) tmp.pop tmp pop 0 G[u] = tmp G[u] G u tmp d = bfs(G, 1) d bfs(G, 1) bfs G 1 for node in G:
        print('{0} {1}'.format(node, d[node])) node G print('{0} {1}'.format(node, d[node])) print '{0} {1}'.format(node, d[node]) '{0} {1}'.format '{0} {1}' format node d[node] d node def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d bfs u = tmp.pop(0) tmp.pop(0) u tmp = list(map(int, input().split())) list(map(int, input().split())) tmp G = {} {} G k = tmp.pop(0) tmp.pop(0) k n = int(input()) int(input()) n d = bfs(G, 1) bfs(G, 1) d
def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    d = bfs(G, 1)
    for node in G:
        print('{0} {1}'.format(node, d[node]))