#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 200000;
long long ans;
int l;
int tr[N], ai[N], ord[N];
vector<int> V[N];
int comp(int a, int b) { return (a + n - l - 1) % n < (b + n - l - 1) % n; }
void ADD(int t, int d) {
  for (; t <= n; t += (t & (-t))) tr[t] += d;
}
int SUM(int t) {
  int d = 0;
  for (; t; t -= (t & (-t))) d += tr[t];
  return d;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> ai[i], V[ai[i]].push_back(i);
  int s = 1;
  for (int p = 1; p <= 100000; ++p) {
    sort(V[p].begin(), V[p].end(), comp);
    for (int q = 0; q < V[p].size(); ++q) {
      int w = V[p][q];
      if (w > l)
        ans += (w - l) - (SUM(w) - SUM(l));
      else
        ans += w + (n - l) - SUM(w) - ((s - 1) - SUM(l));
      ADD(w, 1);
      l = w;
      s++;
    }
  }
  cout << ans;
}
