#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
const long long INF = 1e5;
long long n, k, p, a[MAXN], b[MAXN], tree[MAXN * 4], Min[MAXN * 4],
    MinId[MAXN * 4], ans, cnt;
set<long long> s[MAXN];
long long lowbit(long long x) { return x & (-x); }
void add(long long x, long long y) {
  for (long long i = x; i <= n; i += lowbit(i)) {
    tree[i] += y;
  }
}
long long get_sum(long long x) {
  long long ans = 0;
  for (long long i = x; i > 0; i -= lowbit(i)) {
    ans += tree[i];
  }
  return ans;
}
long long calc(long long a, long long b) { return get_sum(b) - get_sum(a - 1); }
int main() {
  while (~scanf("%lld", &n)) {
    for (long long i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      s[a[i]].insert(i);
      add(i, 1);
    }
    long long p = 0;
    for (long long i = 1; i <= INF; i++) {
      while (s[i].size()) {
        set<long long>::iterator it = s[i].lower_bound(p + 1);
        if (it != s[i].end()) {
          long long pos = *it;
          ans += calc(p + 1, pos);
          p = pos;
          add(pos, -1);
          s[i].erase(it);
        } else {
          ans += calc(p + 1, n);
          p = 0;
        }
      }
    }
    printf("%lld\n", ans);
  }
}
