#include <bits/stdc++.h>
using namespace std;
struct BIT {
  vector<int64_t> bit;
  int64_t n;
  BIT(int64_t n_) {
    this->n = n_ + 1;
    bit.resize(n, 0);
  }
  int64_t sum(int64_t index) {
    int64_t val = 0;
    for (++index; index > 0; index -= (index & -index)) {
      val += bit[index];
    }
    return val;
  }
  int64_t sum(int64_t l, int64_t r) { return sum(r) - sum(l - 1); }
  void add(int64_t index, int64_t val) {
    for (++index; index < n; index += (index & -index)) {
      bit[index] += val;
    }
  }
  void createTree(vector<int64_t>& a) {
    for (int64_t i = 0; i < int64_t(a.size()); ++i) {
      add(i, a[i]);
    }
  }
};
const int64_t nax = 1e6 + 100;
set<int64_t> a[nax];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int64_t n;
  cin >> n;
  BIT bt(n);
  for (int64_t i = 0; i < n; ++i) {
    bt.add(i, 1);
    int64_t x;
    cin >> x;
    a[x].insert(i);
  }
  int64_t pos = 0, tot = 0;
  for (int64_t i = 0; i < nax; ++i) {
    while (!a[i].empty()) {
      auto it = a[i].lower_bound(pos);
      if (it == a[i].end()) it = a[i].begin();
      int64_t nxt = *it;
      if (nxt >= pos) {
        tot += bt.sum(pos, nxt);
      } else {
        tot += bt.sum(pos, n - 1) + bt.sum(0, nxt);
      }
      pos = (nxt + 1) % n;
      bt.add(nxt, -1);
      a[i].erase(it);
    }
  }
  cout << tot;
}
