# 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 ascending order (smallest number on top)
3. Calculate the total "distance" cost of all moves required to achieve this arrangement
4. Each move involves taking a card 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 ascending  order (smallest to largest)
3. Distance starts at 0
4. Each move adds to the total distance
5. Example 1:
   Initial sequence: [2] [3] [1] [4]
   Step 1: [] [3] [1, 2] [4]
   Explanation: The "2" moves below "1", and the distance moved is 2. distance=distance+2=2
   Step 2: [] [] [1, 2, 3] [4]
   Explanation: The "3" moves below stack [1, 2], and the distance moved is 1. distance=distance+1=3
   Step 3: [] [] [1, 2, 3, 4] []
   Explanation: The "4" moves below stack [3, 2, 1] , and the distance moved is 2. distance=distance+2=5
   Result: All numbers are stacked vertically in ascending  order: [1, 2, 3, 4]. The total distance moved is 5.

# Visual Patterns
1. Movement Rules:
   - Cards can only be stacked in ascending  order (smaller numbers on top)
   - Each step moves only one card.
   - Each move must result in a valid stack (ascending  order)
   - After determining the position of the smallest card, move the other cards in order to place them below the smallest card.

2. Distance Calculation Rules:
   - The distance is the absolute difference between the current position and the target position index.

3. Step to solve:
   1. First, find the index of the smallest card: min_position = cards.index(min(cards)). After that, all the other cards will be moved to this position.
   2. Once the min_position is determined, it will not be changed.
   3. Then, begin iterating through cards, with number in range(1, len(cards) + 1).
   4. Find the index of the current card being traversed: index = cards.index(num).
   5. Calculate the distance: distance += abs(index - min_position).

# Solution
def solution(cards: list[int]) -> int:
    """
    Based on the visual examples provided, calculate the total distance required to sequentially stack the given cards.

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

    Output:
    - An integer representing the total distance required to fully stack the cards in descending order.
    """
    steps = 0
    position = cards.index(min(cards))
    for num in range(len(cards), 0, -1):
        index = cards.index(num)
        steps += abs(index - position)
    return steps


# Test Script
def test():
    test_cases = [
        ([2, 3, 1, 4], 4),
        ([1, 2, 3, 4], 6),
        ([4, 3, 2, 1], 6),
        ([3, 1, 4, 2], 4),
        ([1], 0),
        ([2, 1], 1),
        ([15, 2, 9, 5, 13, 6, 7, 12, 1, 4, 3, 10, 11, 14, 8], 57),
        ([5, 3, 2, 1, 4, 6], 9),
        ([15, 2, 9, 5, 4, 6, 7, 12, 1, 13, 3, 10, 11, 14, 8], 57),
    ]
    for test_case in test_cases:
        cards = test_case[0][:]  # Create a copy of input
        expected_output = test_case[1]
        
        try:
            output = solution(cards)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`cards`: {test_case[0]}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message
            
        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`cards`: {test_case[0]}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()