def get_weights():
    input()
    return raw_input().split()

#weights: list
def minimam_cost_sort(weights):
    lol = []
    for i in weights:
        if i !=0:
            j = i
            l = [j]
            while weights[j - 1] != 0:
                l.append(weights[j - 1])
                j = weights[j - 1]
                weights[j - 1] = 0
            l.sort()
            lol.append(l)

    lol.sort()
    s = 0
    for l in lol:
        s += get_wol(l)

    print s

def get_wol(l):
    sub = (l[0] - 1) * (len(l) - 1) - (l[0] + 1) * 2
    s = sum(l) + (l[0] * (len(l) - 2))
    if sub > 0:
        return s - sub
    return s

minimam_cost_sort(get_weights())

### FIXED CODE ###

# -*- coding: utf-8 -*-
import copy

class BagsWithWeight(list):
    def __init__(self, l):
        super(BagsWithWeight, self).__init__(l)
        self.sorted_weights = list(sorted(self))

    def get_lists_of_loop(self):
        """???????????????????????????????????????????±??????????
        ????????\??????????????????????????????????????¬??\???????????? index ???????????????????????????
        ??????????????????????????¬??\???????????? index ????????£???????????¨?????????????????????????????????
        ?????????????????§?????????????????????????????¨ 1 ????????????????????????
        ????????????????????????????????? 1 ????????\???????????§??????????????????????????????????????????
        out: ListOfLoops     ??¨?????? loop ????????????
        """
        c_weights = copy.copy(self)
        loops = Loops()
        for i in range(len(self)):
            loop = []
            index = i
            while c_weights[index] != -1:
                loop.append(c_weights[index])
                c_weights[index] = -1
                index = self.get_index_of_sorted_bag(index)
            if loop:
                loops.append(loop)
        return loops

    def get_index_of_sorted_bag(self, index):
        """???????????????????????????????????????????????? index ????????????
        in:  index    int    ??????????????? index ????±?????????????????????? index
        out: int    ?????????????????????????????? index
        """
        return self.sorted_weights.index(self[index])


class Loops(list):
    def append(self, elm):
        elm.sort()
        elm = Loop(elm, self)
        super(Loops, self).append(elm)

    def get_minimum_element(self):
        return min(min(self))

    def get_cost(self):
        return sum(map((lambda loop: loop.get_cost_of_sorting()), self))


class Loop(list):
    def __init__(self, loop, loops):
        super(Loop, self).__init__(loop)
        self.loops = loops
    
    def get_minimum_weight_of_outer(self):
        return self.loops.get_minimum_element()

    def get_cost_of_sorting(self):
        """Loop ?????¨???????°??????????????±??????????
        """
        cost = sum(self) + min(self) * (len(self) - 2)
        cost_reduced = self.get_how_much_outer_minimum_weight_bag_reduces_cost()
        if cost_reduced > 0:
            return cost - cost_reduced
        else:
            return cost


    def get_how_much_outer_minimum_weight_bag_reduces_cost(self):
        """Loops ???????°????????????????????????£???????????§???????????????????±??????????
        List of Loops ??????????°?????????????????´????
        Loop ??????????°?????????????????´???¨??\????????????
        ?????????????????£?????????????????§???????????????????????????
        ??????????????? len(loop) - 1 ??????????????????????????????????°???????????????????????????????
        out: int    ????????§???????????????
        """
        return ((min(self) - self.get_minimum_weight_of_outer()) *
                    (len(self) - 1) -
                    (min(self) + self.get_minimum_weight_of_outer()) * 2)


def get_weights():
    raw_input()
    return map(int, raw_input().split())

bww = BagsWithWeight(get_weights())
loops = bww.get_lists_of_loop()
print loops.get_cost()