#include <bits/stdc++.h>
using namespace std;
long long int dif(long long int a, long long int b) {
  return ((a / b) + (a % b != 0));
}
long long int n, a[100100], last, dn;
set<pair<long long int, long long int> > s[400400];
long long int st[400400], m, ans;
void build(long long int n, long long int l, long long int r) {
  if (l == r) {
    st[n] = 1;
    return;
  }
  long long int mid = (l + r) / 2;
  build(2 * n, l, mid);
  build(2 * n + 1, mid + 1, r);
  st[n] = st[2 * n] + st[2 * n + 1];
}
void upd(long long int n, long long int l, long long int r, long long int p) {
  if (p < l || p > r) return;
  if (l == r && l == p) {
    st[n] = 0;
    return;
  }
  long long int mid = (l + r) / 2;
  upd(2 * n, l, mid, p);
  upd(2 * n + 1, mid + 1, r, p);
  st[n] = st[2 * n] + st[2 * n + 1];
}
long long int qry(long long int n, long long int l, long long int r,
                  long long int f, long long int t) {
  if (f > r || t < l) return 0;
  if (l >= f && r <= t) {
    return st[n];
  }
  long long int mid = (l + r) / 2;
  long long int s1 = qry(2 * n, l, mid, f, t);
  long long int s2 = qry(2 * n + 1, mid + 1, r, f, t);
  return s1 + s2;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long int i = 0; i < 100001; i++) {
    s[i].clear();
  }
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
    s[a[i]].insert({i, 1LL});
    m = max(m, a[i]) + 1;
  }
  build(1LL, 1LL, n);
  last = 1;
  long long int cc = 1;
  for (long long int i = 1; i <= m; i++) {
    dn = 0;
    if (s[i].size() == 0) continue;
    for (auto p : s[i]) {
      if (p.first >= last && dn < s[i].size()) {
        ans += qry(1LL, 1LL, n, last, p.first);
        upd(1LL, 1LL, n, p.first);
        last = p.first;
        p.second = 0;
        dn++;
      }
    }
    if (s[i].size() == dn) continue;
    ans += qry(1LL, 1LL, n, last, n);
    last = 1;
    for (auto p : s[i]) {
      if (p.first >= last && dn < s[i].size()) {
        if (!p.second) break;
        ans += qry(1LL, 1LL, n, last, p.first);
        upd(1LL, 1LL, n, p.first);
        last = p.first;
        p.second = 0;
        dn++;
      }
    }
  }
  cout << ans;
  return 0;
}
