#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
struct Bit {
  vl bit;
  int n;
  Bit(int nn) : n(nn), bit(vl(nn + 2, 0)) {}
  void add(int i, ll v) {
    for (++i; i <= n + 1; i += i & -i) bit[i] += v;
  }
  ll get_cumulative(int i) {
    ll res = 0;
    for (++i; i; i -= i & -i) res += bit[i];
    return res;
  }
  ll get_interval(int a, int b) {
    return get_cumulative(b) - get_cumulative(a - 1);
  }
};
int main() {
  int n;
  cin >> n;
  Bit b(n + 5);
  vl v(n);
  vector<pll> vsorted(n);
  vector<vector<ll>> vs(100005);
  for (int i = (0); i < (n); i++) {
    cin >> v[i];
    vsorted[i] = make_pair(v[i], i);
    vs[v[i]].push_back(i);
  }
  ll counter = 0;
  int lastPos = -1;
  int k = n, rem = 0, lastI = -1, idx = -1;
  while (k--) {
    if (rem == 0) {
      lastI++;
      while (vs[lastI].size() == 0) lastI++;
      idx = upper_bound(begin(vs[lastI]), end(vs[lastI]), lastPos) -
            vs[lastI].begin();
      if (idx == vs[lastI].size()) idx = 0;
      rem = vs[lastI].size() - 1;
    } else {
      rem--;
      idx = (idx + 1) % vs[lastI].size();
    }
    ll tar = vs[lastI][idx];
    ll steps;
    if (lastPos < tar) {
      steps = tar - lastPos;
      steps -= b.get_interval(n - 1 - tar, n - 1 - lastPos - 1);
    } else {
      steps = tar + n - lastPos;
      steps -= b.get_interval(n - 1 - (n - 1), n - 1 - lastPos - 1);
      steps -= b.get_interval(n - 1 - tar, n - 1);
    }
    counter += steps;
    b.add(n - 1 - tar, 1);
    lastPos = tar;
  }
  cout << counter << endl;
}
