from sys import stdin

def bfs(graph,start,d):
    queue = [start]
    explored = []

    while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n'


def main():
    n = int(stdin.readline())

    graph_dict = {}

    d = [-1]*n

    for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

    d[0] = 0
    bfs(graph_dict,1,d)

    for i in xrange(n):
        print str(i+1) + ' ' + str(d[i])

main() from sys import stdin def bfs(graph,start,d):
    queue = [start]
    explored = []

    while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n' bfs queue = [start] queue [start] start explored = [] explored [] while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n' len(queue) > 0 len(queue) len queue 0 node = queue.pop(0) node queue.pop(0) queue.pop queue pop 0 if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n' node not in explored node explored neighbours = graph[node] neighbours graph[node] graph node for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

             neighbour neighbours if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

             neighbour not in queue neighbour queue if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

             d[neighbour-1] == -1 d[neighbour-1] d neighbour-1 neighbour 1 -1 d[neighbour - 1] = d[node - 1] + 1 d[neighbour - 1] d neighbour - 1 neighbour 1 d[node - 1] + 1 d[node - 1] d node - 1 node 1 1 queue.append(neighbour) queue.append queue append neighbour explored.append(node) explored.append explored append node graph graph start start d d def main():
    n = int(stdin.readline())

    graph_dict = {}

    d = [-1]*n

    for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

    d[0] = 0
    bfs(graph_dict,1,d)

    for i in xrange(n):
        print str(i+1) + ' ' + str(d[i]) main n = int(stdin.readline()) n int(stdin.readline()) int stdin.readline() stdin.readline stdin readline graph_dict = {} graph_dict {} d = [-1]*n d [-1]*n [-1] -1 n for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

     i xrange(n) xrange n int(s) for s in stdin.readline().split()[2:] for s in stdin.readline().split()[2:] s stdin.readline().split()[2:] stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split 2: 2 for s in stdin.readline().split()[2:] int(s) int s entry = [int(s) for s in stdin.readline().split()[2:]] entry [int(s) for s in stdin.readline().split()[2:]] graph_dict[i+1] = entry graph_dict[i+1] graph_dict i+1 i 1 entry d[0] = 0 d[0] d 0 0 bfs(graph_dict,1,d) bfs graph_dict 1 d for i in xrange(n):
        print str(i+1) + ' ' + str(d[i]) i xrange(n) xrange n print str(i+1) + ' ' + str(d[i]) str(i+1) + ' ' str(i+1) str i+1 i 1 ' ' str(d[i]) str d[i] d i main() main def main():
    n = int(stdin.readline())

    graph_dict = {}

    d = [-1]*n

    for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

    d[0] = 0
    bfs(graph_dict,1,d)

    for i in xrange(n):
        print str(i+1) + ' ' + str(d[i]) def main():
    n = int(stdin.readline())

    graph_dict = {}

    d = [-1]*n

    for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

    d[0] = 0
    bfs(graph_dict,1,d)

    for i in xrange(n):
        print str(i+1) + ' ' + str(d[i]) main def bfs(graph,start,d):
    queue = [start]
    explored = []

    while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n' def bfs(graph,start,d):
    queue = [start]
    explored = []

    while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n' bfs
from sys import stdin

def bfs(graph,start,d):
    queue = [start]
    explored = []

    while len(queue) > 0:
        node = queue.pop(0)
#        print 'node ' + str(node)
        if node not in explored:
            neighbours = graph[node]
            for neighbour in neighbours:
                if neighbour not in queue:
                    # print 'neighbour ' + str(neighbour)
                    if d[neighbour-1] == -1:
                        d[neighbour - 1] = d[node - 1] + 1
                        queue.append(neighbour)
                        # print 'queue ' + str(queue)

            explored.append(node)

        # print 'd ' + str(d)
        # print '\n'


def main():
    n = int(stdin.readline())

    graph_dict = {}

    d = [-1]*n

    for i in xrange(n):
        entry = [int(s) for s in stdin.readline().split()[2:]]
        graph_dict[i+1] = entry

    d[0] = 0
    bfs(graph_dict,1,d)

    for i in xrange(n):
        print str(i+1) + ' ' + str(d[i])

main()
