#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iomanip>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
struct str
{ int st, lower, upper; };

str b[100001];
int n, a[100001], br[100001], d[100001], t = 1, y = 0, e = 0, ok = 0, ans = 0;
map <int, int> m;
void in()
{ scanf("%d", &n);
  for (int i = 0; i < n; i++)
  m[i] = 0;
  
  for (int i = 0; i < n; i++)
  { scanf("%d", &a[i]); br[a[i]]++;
    //if (d[a[i]] == 0) d[a[i]] = t++;
    if (m[a[i]] == 0)
    { /*cout << "a[i] = " << a[i] << " m[a[i]] = " << m[a[i]] << " t = " << t << endl;*/ m[a[i]] = t++; }
   }
}

void make()
{ for (int i = 0; i <= n; i++)
  if (m[i] != 0) d[y++] = i;
  
  /*for (int i = 0; i <= n; i++)
  cout << "m[i] = " << m[i] << endl;
  
  cout << "t = " << t << endl;
  
  cout << "y = " << y <<endl;
  for (int i = 0; i < y ; i++)
  cout << "d[i] = " << d[i] << endl;*/
  
  b[d[0]].st = d[0];
  b[d[0]].lower = 0;
  b[d[0]].upper = br[d[0]];
  
  for (int i = 1; i < y; i++)
  { b[d[i]].st = d[i];
    b[d[i]].lower = b[d[i - 1]].upper;
    b[d[i]].upper = b[d[i]].lower + br[d[i]];
   }
  
  //for (int i = 0; i < y; i++)
  //cout << b[d[i]].st << ' ' << b[d[i]].lower << ' ' << b[d[i]].upper << endl;
  
  while (ok == 0)
  { for (int i = 0; i < n; i++)
    { if (i == b[d[e]].upper) e++;
      
      if (a[i] != b[d[e]].st)
      { for (int j = b[a[i]].lower; j < n; j++)
        { if (a[j] == b[d[e]].st)
          { int r = a[i];
            a[i] = a[j];
            a[j] = r;
            ans++;
            break;
           }
         }
       }
     }
    
    e = 0;
    for (int i = 0; i < n; i++)
    { if (i == b[d[e]].upper) e++;
      
      if (a[i] != b[d[e]].st)
      { ok = -1; break; }
     }
    
    if (ok == -1) ok = 0;
    else
    { ok = 1; break; }
   }
  
  cout << ans << endl;
}

int main()
{ 
  in();
  make();
  
  //cin >> n;
  return 0;
}
/*
9
1 2 1 3 1 3 2 4 5

10
5 1 9 2 10 3 7 4 6 5

20
10 1 9 2 8 3 7 4 6 5 20 11 19 12 18 13 17 14 16 15
*/
