#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 7;
int n;
int bit[MAXN], a[MAXN];
set<int> st[MAXN];
int lowbit(int x) { return x & (-x); }
void modify(int x, int c) {
  while (x <= n) {
    bit[x] += c;
    x += lowbit(x);
  }
}
int query(int x) {
  long long sum = 0;
  while (x > 0) {
    sum += bit[x];
    x -= lowbit(x);
  }
  return sum;
}
int main() {
  while (scanf("%d", &n) != EOF) {
    memset(bit, 0, sizeof(bit));
    for (int i = 1; i <= n; ++i) st[i].clear();
    for (int i = 1; i <= n; ++i) {
      scanf("%d", a + i);
      st[a[i]].insert(i);
    }
    for (int i = 1; i <= n; ++i) modify(i, 1);
    int cnt = n, now = 1, mi = 0;
    long long res = 0;
    while (cnt > 0) {
      while (st[mi].empty()) mi++;
      if (st[mi].lower_bound(now) != st[mi].end()) {
        int la = now;
        now = *st[mi].lower_bound(now);
        res += query(now) - query(la - 1);
      } else {
        int la = now;
        now = *st[mi].lower_bound(1);
        res += query(n) - query(la - 1) + query(now);
      }
      modify(now, -1);
      st[mi].erase(now);
      cnt--;
    }
    printf("%lld\n", res);
  }
  return 0;
}
