#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long MAX = 1 << 17;
long long tab[MAX];
set<long long> pos[MAX];
long long t[2 * MAX];
long long ans = 0;
void update(long long u, long long c) {
  for (t[u += MAX] = c; u > 1; u >>= 1) t[u >> 1] = t[u] + t[u ^ 1];
}
long long query(long long u, long long v) {
  long long sum = 0;
  for (u += MAX, v += MAX + 1; u < v; u >>= 1, v >>= 1) {
    if (u & 1) sum += t[u++];
    if (v & 1) sum += t[--v];
  }
  return sum;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++)
    cin >> tab[i], update(i, 1), pos[tab[i]].insert(i);
  sort(tab + 1, tab + n + 1);
  long long last = 1;
  for (long long i = 1; i <= n; i++) {
    auto x = pos[tab[i]].rbegin();
    auto val = *x;
    if (val < last) {
      ans += query(last, n);
      long long ktory = *pos[tab[i]].begin();
      ans += query(1, ktory);
      pos[tab[i]].erase(ktory);
      update(ktory, 0);
      last = ktory + 1;
    } else {
      long long ktory = *pos[tab[i]].lower_bound(last);
      ans += query(last, ktory);
      pos[tab[i]].erase(ktory);
      update(ktory, 0);
      last = ktory + 1;
    }
  }
  cout << ans;
  return 0;
}
