#include <stdio.h>
#include <limits.h>
#include <string.h>

#define MAX 1000
#define INF INT_MAX

int solve(int*, int);
int check(int*,int);

int used[MAX];

int main(){
  int n, i, arr[MAX];

  scanf("%d" ,&n);
  for(i = 0 ; i < n ; i++){
    scanf("%d" ,arr + i);
  }
  memset(used, 0, sizeof(used));
  printf("%d\n" ,solve(arr, n));

  return 0;
}

int check(int arr[], int n){
  int i;

  for(i = 0 ; i < n ; i++){
    if(arr[i] != i+1){
      return 0;
    }
  }
  return 1;
}

int solve(int arr[], int n){
  int i, j, res = 0, tmp, min = INF, pos, flg;

  for(i = 0 ; i < n-1 ; i++){
    flg = 0;
    if(arr[i] == i+1){
      used[i] = 1;
      continue;
    }

    for(j = i+1 ; j < n ; j++){
      if(arr[j] == j+1 || used[j]){
        used[j] = 1;
        continue;
      }

      pos = -1;
      if(arr[i] == j+1 && arr[j] == i+1){
        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        res += arr[i] + arr[j];
        flg = 1;
        break;
      }else{
        if(arr[i] + arr[j] < min){
          min = arr[i] + arr[j];
          pos = j;
        }
      }
    }
    if(pos != -1 && !flg){
      tmp = arr[i];
      arr[i] = arr[j];
      arr[j] = tmp;
      res += arr[i] + arr[j];
    }
  }

  if(check(arr, n)){
    return res;
  }else{
    return res += solve(arr, n);
  }
}