#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long b, long long p = MOD) {
  a = a % p;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const long long N = 1e6 + 2;
struct FenwickTree {
  vector<long long> bit;
  long long n;
  void init(long long n) {
    this->n = n;
    bit.assign(n, 0);
  }
  long long sum(long long r) {
    long long ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }
  void add(long long idx, long long delta) {
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
  long long sum(long long l, long long r) { return sum(r) - sum(l - 1); }
  void init(vector<long long> a) {
    init(a.size());
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
} fw;
vector<long long> v[100005];
long long dis(long long curr, long long tar, long long n) {
  if (tar >= curr) {
    return abs(tar - curr + 1);
  }
  return abs(n - curr + 1 + tar);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v[x].push_back(i);
  }
  fw.init(n + 2);
  long long curr = 0;
  long long ans = 0;
  for (long long i = 1; i <= 100000; i++) {
    if (v[i].size() == 0) continue;
    sort((v[i]).begin(), (v[i]).end(), [&](long long x, long long y) {
      return dis(curr, x, n) < dis(curr, y, n);
    });
    for (long long j = 0; j < v[i].size(); j++) {
      long long pos = v[i][j];
      long long val = 0;
      if (pos >= curr) {
        val = pos - curr + 1;
        ans += val - (fw.sum(curr, pos));
        fw.add(pos, 1);
      } else {
        val = n - curr + 1 + pos;
        ans += val - (fw.sum(curr, n - 1));
        ans -= (fw.sum(0, pos));
        fw.add(pos, 1);
      }
      curr = pos + 1;
      if (curr >= n) curr = 0;
    }
  }
  cout << ans;
}
