#include <bits/stdc++.h>
using namespace std;
set<int> tong[101010];
set<int>::iterator tongitr;
int main() {
  int T, x, p = 999999, qk = 0, qt = 0;
  long long ans = 0, dis = 0;
  scanf("%d", &T);
  for (int i = 0; i < T; i++) {
    scanf("%d", &x);
    tong[x].insert(i);
  }
  for (int i = 0; i <= 100000; i++) {
    if (tong[i].empty()) continue;
    qk = 0;
    for (tongitr = tong[i].begin(); tongitr != tong[i].end(); tongitr++) {
      if (*tongitr < p) {
        qk = 1;
        qt = *tongitr;
        ans += 1;
      }
      if (qk == 0) qt = *tongitr;
      ans += dis;
    }
    if (qk == 1) dis++;
    p = qt;
  }
  printf("%I64d\n", ans);
  return 0;
}
