#include <bits/stdc++.h>
using namespace std;
struct FenwickTree {
  int n;
  vector<int> tree;
  FenwickTree(int n) : n(n) { tree.assign(n + 1, 0); }
  int query(int l, int r) { return query(r) - query(l - 1); }
  int query(int r) {
    int s = 0;
    for (; r > 0; r -= (r & (-r))) s += tree[r];
    return s;
  }
  void update(int i, int v) {
    for (; i <= n; i += (i & (-i))) tree[i] += v;
  }
};
int a[100000], ra[100000];
set<int> pos[100000 + 1];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    pos[a[i]].insert(i + 1);
  }
  FenwickTree ft(n);
  for (int i = 0; i < n; i++) ft.update(i + 1, 1);
  int prevpos = 0;
  long long res = 0;
  int val = 0;
  int doneforval = 0;
  auto it = pos[0].begin();
  for (int i = 0; i < n; i++) {
    while (doneforval == pos[val].size()) {
      val++;
      doneforval = 0;
    }
    if (!doneforval)
      it = upper_bound(pos[val].begin(), pos[val].end(), prevpos);
    else
      ++it;
    if (it == pos[val].end()) {
      it = pos[val].begin();
    }
    int nextpos = *it;
    doneforval++;
    if (nextpos >= prevpos) {
      res += ft.query(nextpos) - ft.query(prevpos);
    } else {
      res += ft.query(n) - ft.query(prevpos) + ft.query(nextpos);
    }
    prevpos = nextpos;
    ft.update(prevpos, -1);
  }
  cout << res << endl;
  return 0;
}
