#include <bits/stdc++.h>
using namespace std;
int n, k;
pair<pair<int, int>, int> ar[100003];
int tree[100003];
set<int> all;
set<int> myset[100003];
int query(int x) {
  int add = 0;
  while (x) {
    add += tree[x];
    x -= (x & (-x));
  }
  return add;
}
void update(int x) {
  while (x <= n) {
    tree[x]++;
    x += (x & (-x));
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    all.insert(i);
    scanf("%d", &ar[i].first.first);
    ar[i].first.second = i;
  }
  k = 1;
  sort(ar + 1, ar + n + 1);
  for (int i = 1, j; i <= n; i = j, k++) {
    j = i;
    while (j <= n && ar[i].first.first == ar[j].first.first) {
      ar[j].second = k;
      myset[k].insert(ar[j++].first.second);
    }
  }
  long long ans = 0;
  int curr = 1;
  set<int>::iterator it, it2;
  for (int i = 1; i < k; i++) {
    int index, oldcurr;
    while (!myset[i].empty()) {
      it = myset[i].lower_bound(curr);
      if (it != myset[i].end()) {
        index = (*it);
        ans += index - curr;
        ans -= query(index) - query(curr);
      } else {
        it = myset[i].begin();
        index = (*it);
        ans += index + n - curr;
        ans -= query(n) - query(curr) + query(index);
      }
      curr = index;
      oldcurr = curr;
      update(curr);
      ans++;
      it2 = all.upper_bound(curr);
      if (it2 != all.end())
        curr = (*it2);
      else {
        it2 = all.begin();
        curr = (*it2);
      }
      all.erase(oldcurr);
      myset[i].erase(it);
    }
  }
  printf("%I64d\n", ans);
}
