import std.array, std.stdio, std.conv, std.string, std.math, std.random, std.range,std.functional;
import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.comparison;
 
long count;
 
void main()
{
    readln();
    int[] list = readln().split.to!(int[]);
    auto sortedList = list.dup.sort();

    foreach(i; 0..sortedList.length)
    {
        long minIndex = -1;
        while(sortedList[i] != list[i])
        {
            if(minIndex == -1)
                minIndex = list.countUntil(sortedList[i]);
            long swapIndex = list.countUntil(sortedList[minIndex]);
            count += list[minIndex] + list[swapIndex];

            auto tmp = list[minIndex];
            list[minIndex] = list[swapIndex];
            list[swapIndex] = tmp;
            minIndex = swapIndex;
        }
    }
    writeln(count);
}
 
sizediff_t minIndex(alias pred="a<b", R) (R list)
    if(isForwardRange!R && !isInfinite!R && is(typeof(binaryFun!pred(list.front, list.front))))
{
    assert(!list.empty);
    typeof(list.front) min = list.front;
    sizediff_t minPos;
    sizediff_t curPos;
    for(list.popFront(); !list.empty; list.popFront())
    {
        curPos++;
 
        if(binaryFun!pred(list.front, min))
        {
            min = list.front;
            minPos = curPos;
        }
    }
    return minPos;
}
