#include <bits/stdc++.h>
using namespace std;
const int nstart = 10;
const int maxn = 100100;
int n;
int a[maxn];
int v[maxn];
vector<int> pos[maxn];
int B[maxn];
void update(int i, int d) {
  i += nstart;
  while (i < maxn) B[i] += d, i += i & -i;
}
int query(int i) {
  int r = 0;
  i += nstart;
  while (i > 0) r += B[i], i -= i & -i;
  return r;
}
inline int dist(int a, int b) {
  if (b >= a) {
    return query(b) - query(a - 1);
  }
  return query(n) - query(a - 1) + query(b);
}
int main(int argc, char *argv[]) {
  cin >> n;
  {
    for (int i = 0; i < n; i++)
      cin >> a[i], v[i] = a[i], pos[a[i]].push_back(i);
    sort(v, v + n);
    memset(B, 0, sizeof(B));
    for (int i = 0; i < n; i++) update(i, 1);
    long long r = 0;
    int p = -1;
    for (int i = 0; i < n;) {
      int ii = i;
      while (i < n && v[i] == v[ii]) i++;
      int cnt = i - ii;
      int x = v[ii];
      int next_idx =
          (lower_bound(pos[x].begin(), pos[x].end(), p) - pos[x].begin()) % cnt;
      for (int j = 0; j < cnt; j++) {
        r += dist(p, pos[x][next_idx]);
        p = pos[x][next_idx];
        update(p, -1);
        next_idx = (next_idx + 1) % cnt;
      }
    }
    cout << r << endl;
  }
  return 0;
}
