# Problem Description
This is a card stacking problem where we need to:
1. Take a sequence of numbered cards arranged horizontally
2. Stack them vertically in descending order (largest number on top)
3. Calculate the total "distance" cost of all moves required to achieve this arrangement
4. Each move involves taking a stack (single card or multiple cards) and moving it to combine with another card/stack

# Visual Facts
1. Input is represented as horizontal sequence of numbered cards
2. Final output is always a vertical stack with numbers in descending order (largest to smallest)
3. Distance starts at 0
4. Cards can be moved individually or as stacks
5. Each move adds to the total distance
6. In both examples, the final arrangement is identical (4,3,2,1 from top to bottom)
7. First example starts with [2,3,1,4] and ends with distance=5
8. Second example starts with [4,3,2,1] and ends with distance=3
9. Distance increments differ between examples (Example 1: +2,+1,+2; Example 2: +1,+1,+1)
10. Example 1:
   Initial sequence: [2] [3] [1] [4]
   Step 1: [2, 1] [3] [] [4]
   Explanation: The "1" moves below "2", and the distance moved is 2. distance=distance+2=2
   Step 2: [] [3, 2, 1] [] [4]
   Explanation: The stack [2, 1] moves below "3", and the distance moved is 1. distance=distance+1=3
   Step 3: [] [] [] [4, 3, 2, 1]
   Explanation: The stack [3, 2, 1] moves below "4", and the distance moved is 2. distance=distance+2=5
   Result: All numbers are stacked vertically in descending order: [4, 3, 2, 1]. The total distance moved is 5.

# Visual Patterns
1. Movement Rules:
   - Cards can only be stacked in descending order (larger numbers on top)
   - When moving cards, you can move either a single card or an already-formed stack
   - Each move must result in a valid stack (descending order)
   - The first card to move is the smallest card（min(cards)）.

2. Distance Calculation Rules:
   - Distance increases based on the horizontal gap between positions
   - When moving cards/stacks between adjacent positions, distance = 1
   - When moving cards/stacks across multiple positions, distance = number of positions crossed
   - Distance is measured from the source position to destination position

3. Strategy Patterns:
   - From the card numberd 1, we move it to the card numbered 2, forming a stack [2, 1]
   - Then we move the stack to the card numbered 3, forming a stack [3, 2, 1], and so on.
   - For the first example, we move [1] to [2], cost a distance of 2. then [2, 1] to [3], cost a distance of 1. and then [3, 2, 1] to [4], cost a distance of 2. Total distance is 5.

4. Card number pattern.
   - The card numbers range from 1 to len(cards)
   - In each round, the number to find is the number found in the previous round plus 1. For example:
      In the first round, find 2 and move 1 below 2.
      In the second round, find 3 and move [2, 1] below 3.
      In the third round, find 4 and move [3, 2, 1] below 4.
      In the fourth round, find 5 and move [4, 3, 2, 1] below 5.
      And so on.

# Solution
def solution(numbers: list[int]) -> int:
    """
    Based on the visual examples provided, calculate the total distance required to perform the operations

    Input:
    - numbers: A list of integers representing a sequence of numbers. (len(numbers) >= 1)

    Output:
    - An integer representing the total distance required to perform the operations
    """
    steps = 0
    current_position = numbers.index(min(numbers))
    for num in range(1, len(numbers) + 1):
        index = numbers.index(num)
        steps += abs(index - current_position) 
        current_position = index
    return steps


# Test Script
def test():
    test_cases = [
        ([2, 3, 1, 4], 5),
        ([1, 2, 3, 4], 3),
        ([4, 3, 2, 1], 3),
        ([3, 1, 4, 2], 7),
        ([1], 0),
        ([2, 1], 1),
        ([15, 2, 9, 5, 13, 6, 7, 12, 1, 4, 3, 10, 11, 14, 8], 86),
        ([5, 3, 2, 1, 4, 6], 14),
        ([15, 2, 9, 5, 4, 6, 7, 12, 1, 13, 3, 10, 11, 14, 8], 80),
    ]
    for numbers, expected_output in test_cases:
        original_numbers = numbers[:]
        try:
            output = solution(numbers)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`numbers`: {original_numbers}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`numbers`: {original_numbers}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()