#include <bits/stdc++.h>
using namespace std;
set<int> s[110000];
map<int, int> num;
int n, a[110000], c[110000];
int lowbit(int x) { return x & -x; }
void add(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ret = 0;
  while (x) {
    ret += c[x];
    x -= lowbit(x);
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  n++;
  a[1] = 0;
  for (int i = 2; i <= n; i++) {
    add(i, 1);
    scanf("%d", &a[i]);
    s[a[i]].insert(i);
    num[a[i]]++;
  }
  int k = 1;
  long long ans = 0;
  for (int i = 2; i <= n; i++) {
    pair<int, int> e = *num.begin();
    auto it = s[e.first].upper_bound(k);
    if (it != s[e.first].end())
      ans += sum(*it) - sum(k);
    else {
      it = s[e.first].begin();
      ans += sum(n) - sum(k) + sum(*it);
    }
    k = *it;
    add(*it, -1);
    s[e.first].erase(it);
    num[e.first]--;
    if (!num[e.first]) num.erase(num.begin());
  }
  cout << ans;
  return 0;
}
