#include <bits/stdc++.h>
using namespace std;
set<int> ps, s[100200];
int n;
int a[200000], d[200000], fn[200000];
void md(int x, int y) {
  while (x <= n) {
    fn[x] += y;
    x = (x | (x - 1)) + 1;
  }
}
int fsum(int x) {
  int s = 0;
  while (x >= 1) {
    s += fn[x];
    x = (x & (x - 1));
  }
  return s;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    d[i] = a[i];
    s[a[i]].insert(i);
    ps.insert(i);
  }
  sort(d + 1, d + n + 1);
  int l = 1;
  long long ans = 0;
  for (int i = 1; i <= n; i++) md(i, 1);
  for (int i = 1; i <= n; i++) {
    int x = d[i];
    set<int>::iterator it = s[x].lower_bound(l);
    if (it == s[x].end()) it = s[x].begin();
    int nl = *it;
    if (l <= nl)
      ans += fsum(nl) - fsum(l - 1);
    else
      ans += fsum(n) - fsum(l - 1) + fsum(nl);
    md(nl, -1);
    ps.erase(nl);
    it = ps.upper_bound(nl);
    if (it == ps.end()) it = ps.begin();
    l = *it;
  }
  cout << ans << "\n";
  return 0;
}
