#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int b[maxn];
int c[maxn];
vector<int> vi[maxn];
int n;
int sum(int i) {
  int res = 0;
  while (i > 0) {
    res += c[i];
    i -= i & -i;
  }
  return res;
}
void add(int i, int x) {
  while (i <= n) {
    c[i] += x;
    i += i & -i;
  }
}
int main() {
  while (~scanf("%d", &n)) {
    int pos = 0;
    for (int i = 0; i < maxn; i++) {
      vi[i].clear();
    }
    memset(c, 0, sizeof(c));
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      b[i] = a[i];
    }
    for (int i = 0; i < n; i++) {
      vi[a[i]].push_back(i + 1);
    }
    sort(b, b + n);
    long long ans = 0;
    int now = 0;
    for (int i = 0; i < n;) {
      pos =
          upper_bound(vi[b[i]].begin(), vi[b[i]].end(), now) - vi[b[i]].begin();
      if (pos == 0) {
        int sz = vi[b[i]].size() - 1;
        int pos = vi[b[i]][sz];
        ans += pos - now - (sum(pos) - sum(now));
        now = pos;
      } else {
        pos = vi[b[i]][pos - 1];
        ans += n - sum(n) - (now - pos) + sum(now) - sum(pos);
        now = pos;
      }
      for (int j = 0; j < vi[b[i]].size(); j++) add(vi[b[i]][j], 1);
      i += vi[b[i]].size();
    }
    printf("%I64d\n", ans);
  }
}
