def manhattan_distance(board_state, size=3):
    """Sum up the manhattan distance of all numbers in the board."""
    diff = 0
    for y in range(size):
        for x in range(size):
            idx = board_state[y][x] - 1
            if idx != 8:
                diff += abs(idx % size - x) + abs(idx // size - y)
    return diff