#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct bit {
  int t[N], n;
  void init(int _n) {
    n = _n;
    memset(t, 0, sizeof(t));
  }
  void update(int pos, int val) {
    while (pos <= n) {
      t[pos] += val;
      pos += pos & -pos;
    }
  }
  int get(int pos) {
    int ret = 0;
    while (pos) {
      ret += t[pos];
      pos -= pos & -pos;
    }
    return ret;
  }
  int get(int l, int r) {
    if (l > r) {
      return get(r) + get(n) - get(l - 1);
    } else {
      return get(r) - get(l - 1);
    }
  }
};
int n, a[N];
vector<int> val[N];
int idx = 1;
bit BIT;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    val[a[i]].push_back(i);
  }
  BIT.init(n);
  for (int i = 1; i <= n; ++i) BIT.update(i, +1);
  long long ret = 0;
  for (int i = 1; i <= 100000; ++i) {
    int st = idx;
    for (auto u : val[i]) {
      if (u >= st) {
        ret += BIT.get(idx, u);
        BIT.update(u, -1);
        idx = u + 1;
        if (idx > n) idx = 1;
      }
    }
    for (auto u : val[i]) {
      if (u < st) {
        ret += BIT.get(idx, u);
        BIT.update(u, -1);
        idx = u + 1;
        if (idx > n) idx = 1;
      }
    }
  }
  cout << ret << endl;
  return 0;
}
