#include <bits/stdc++.h>
using namespace std;
using dbl = double;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
template <class Val>
struct BinaryIndexedTree {
  int n;
  vector<Val> t;
  BinaryIndexedTree() {}
  BinaryIndexedTree(int _n) : n(_n + 1), t(_n + 1) {}
  void add(int k, Val val) {
    k++;
    while (k < n) {
      t[k] += val;
      k += (k & -k);
    }
  }
  void set(int k, Val val) {
    add(k, -sum(k, k + 1));
    add(k, val);
  }
  Val sum(int k) {
    Val r = 0;
    while (k > 0) {
      r += t[k];
      k -= (k & -k);
    }
    return r;
  }
  Val sum(int l, int r) { return sum(r) - sum(l); }
};
set<int> P[100001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vi A(N);
  for (int(i) = (0); (i) < (int)(N); ++(i)) {
    cin >> A[i];
    P[A[i]].insert(i);
  }
  sort((A).begin(), (A).end());
  ll ans = 0;
  BinaryIndexedTree<int> bit(N);
  for (int(i) = (0); (i) < (int)(N); ++(i)) bit.add(i, 1);
  int p = 0, q;
  for (auto &(a) : (A)) {
    auto it = P[a].lower_bound(p);
    if (it == P[a].end()) {
      it = P[a].begin();
      q = *it;
      ans += bit.sum(p, N);
      ans += bit.sum(0, q + 1);
    } else {
      q = *it;
      ans += bit.sum(p, q + 1);
    }
    bit.add(q, -1);
    p = (q + 1) % N;
    P[a].erase(it);
  }
  cout << ans << endl;
}
