#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;

template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
 
#ifdef LOCAL 
#include "C:\CP\debug.h"
#else
#define debug(...)
#endif

using ll = long long;
const char nl = '\n';
#define fi first
#define se second
#define pb push_back
typedef vector<int> vi;
#define sz(v) (int)(v.size())
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define unq(x) sort(all(x)) , x.erase(unique(all(x)),x.end())

#include <vector>
#include <algorithm>

int minSwapsToSort(std::vector<int>& nums) {
    int n = nums.size();
    std::vector<std::pair<int, int>> arrPos(n);
    for (int i = 0; i < n; i++) {
        arrPos[i] = {nums[i], i};
    }
    std::sort(arrPos.begin(), arrPos.end());
    std::vector<bool> visited(n, false);
    int swaps = 0;
    for (int i = 0; i < n; i++) {
        if (visited[i] || arrPos[i].second == i)
            continue;
        int cycleSize = 0;
        int j = i;
        while (!visited[j]) {
            visited[j] = true;
            j = arrPos[j].second;
            cycleSize++;
        }
        if (cycleSize > 0)
            swaps += (cycleSize - 1);
    }
    return swaps;
}


void leftCyclicShift(std::vector<int>& nums) {
    if (nums.empty()) return;
    int first = nums[0];
    for (int i = 0; i < nums.size() - 1; i++) {
        nums[i] = nums[i + 1];
    }
    nums[nums.size() - 1] = first;
}

 
void Solve() {
   int n; cin >> n;
   vector<int> arr(n); for(auto& x: arr)cin >> x;
   vector<int> brr(arr);
   sort(all(brr));
   if(arr == brr){
      cout << 0 << nl;
      return;
   }
   int ans = INT_MAX;
   for(int shifts = 1; shifts < n; ++shifts) {
      leftCyclicShift(arr);
      if(arr == brr){
         ans = min(ans,shifts);
         break;
      }
      int mn = minSwapsToSort(arr);
      ans = min(ans,shifts + mn);
   }
   cout << ans << nl;
}
 
signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    #ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
    #endif


    int tt = 1;
    //cin >> tt;
    while ( tt--) {
      Solve();
    } 
    return 0;
}