if __name__ == '__main__':
    n = int(input())
    graph = [[0 for i in range(n)] for _ in range(n)]
    frist = [0 for _ in range(n)]
    end = [0 for _ in range(n)]
    visit = [False for _ in range(n)]
    # get the group
    for i in range(n):
        datas = [int(num) for num in input().split(' ')]
        if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

    queue = [0]
    visit = [False for _ in range(n)]
    visit[0] = True
    depths = [0 for _ in range(n)]
    while len(queue) != 0:
        point = queue.pop(0)
        for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
        if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

    for i in range(n):
        print('%d %d' % (i + 1, depths[i])) if __name__ == '__main__':
    n = int(input())
    graph = [[0 for i in range(n)] for _ in range(n)]
    frist = [0 for _ in range(n)]
    end = [0 for _ in range(n)]
    visit = [False for _ in range(n)]
    # get the group
    for i in range(n):
        datas = [int(num) for num in input().split(' ')]
        if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

    queue = [0]
    visit = [False for _ in range(n)]
    visit[0] = True
    depths = [0 for _ in range(n)]
    while len(queue) != 0:
        point = queue.pop(0)
        for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
        if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

    for i in range(n):
        print('%d %d' % (i + 1, depths[i])) __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input [0 for i in range(n)] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0 for i in range(n)] graph = [[0 for i in range(n)] for _ in range(n)] graph [[0 for i in range(n)] for _ in range(n)] 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 frist = [0 for _ in range(n)] frist [0 for _ in range(n)] 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 end = [0 for _ in range(n)] end [0 for _ in range(n)] False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False visit = [False for _ in range(n)] visit [False for _ in range(n)] for i in range(n):
        datas = [int(num) for num in input().split(' ')]
        if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

     i range(n) range n int(num) for num in input().split(' ') for num in input().split(' ') num input().split(' ') ().split () input split ' ' for num in input().split(' ') int(num) int num datas = [int(num) for num in input().split(' ')] datas [int(num) for num in input().split(' ')] if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

     datas[1] > 0 datas[1] datas 1 0 for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

     j datas[2:] datas 2: 2 graph[datas[0] - 1][j - 1] = 1 graph[datas[0] - 1][j - 1] [datas[0] - 1] graph datas[0] - 1 datas[0] datas 0 1 j - 1 j 1 1 queue = [0] queue [0] 0 False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False visit = [False for _ in range(n)] visit [False for _ in range(n)] visit[0] = True visit[0] visit 0 True 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 depths = [0 for _ in range(n)] depths [0 for _ in range(n)] while len(queue) != 0:
        point = queue.pop(0)
        for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
        if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

     len(queue) != 0 len(queue) len queue 0 point = queue.pop(0) point queue.pop(0) queue.pop queue pop 0 for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
         i range(n) range n if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
         graph[point][i] == 1 and visit[i] is False graph[point][i] == 1 graph[point][i] [point] graph point i 1 visit[i] is False visit[i] visit i False queue.append(i) queue.append queue append i visit[i] = True visit[i] visit i True depths[i] = depths[point] + 1 depths[i] depths i depths[point] + 1 depths[point] depths point 1 if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

     len(queue) == 0 len(queue) len queue 0 for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

     i range(n) range n if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

     visit[i] is False visit[i] visit i False queue.append(i) queue.append queue append i depths[i] = -1 depths[i] depths i -1 visit[i] = True visit[i] visit i True for i in range(n):
        print('%d %d' % (i + 1, depths[i])) i range(n) range n print('%d %d' % (i + 1, depths[i])) print '%d %d' % (i + 1, depths[i]) '%d %d' (i + 1, depths[i]) i + 1 i 1 depths[i] depths i frist = [0 for _ in range(n)] [0 for _ in range(n)] frist visit = [False for _ in range(n)] [False for _ in range(n)] visit graph = [[0 for i in range(n)] for _ in range(n)] [[0 for i in range(n)] for _ in range(n)] graph visit = [False for _ in range(n)] [False for _ in range(n)] visit n = int(input()) int(input()) n point = queue.pop(0) queue.pop(0) point end = [0 for _ in range(n)] [0 for _ in range(n)] end datas = [int(num) for num in input().split(' ')] [int(num) for num in input().split(' ')] datas depths = [0 for _ in range(n)] [0 for _ in range(n)] depths queue = [0] [0] queue
if __name__ == '__main__':
    n = int(input())
    graph = [[0 for i in range(n)] for _ in range(n)]
    frist = [0 for _ in range(n)]
    end = [0 for _ in range(n)]
    visit = [False for _ in range(n)]
    # get the group
    for i in range(n):
        datas = [int(num) for num in input().split(' ')]
        if datas[1] > 0:
            for j in datas[2:]:
                graph[datas[0] - 1][j - 1] = 1

    queue = [0]
    visit = [False for _ in range(n)]
    visit[0] = True
    depths = [0 for _ in range(n)]
    while len(queue) != 0:
        point = queue.pop(0)
        for i in range(n):
            if graph[point][i] == 1 and visit[i] is False:
                queue.append(i)
                visit[i] = True
                depths[i] = depths[point] + 1
        if len(queue) == 0:
            for i in range(n):
                if visit[i] is False:
                    queue.append(i)
                    depths[i] = -1
                    visit[i] = True

    for i in range(n):
        print('%d %d' % (i + 1, depths[i]))
