#include <bits/stdc++.h>
using namespace std;
int n, bin[200005] = {0};
vector<int> v[200005];
void update(int x, int y) {
  while (x <= n) {
    bin[x] += y;
    x += x & (-x);
  }
}
int answer(int x) {
  int sum = 0;
  while (x > 0) {
    sum += bin[x];
    x -= x & (-x);
  }
  return sum;
}
int main() {
  int i, j, pos = 0, curr = 0, temp = 0, x = 0;
  long long ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x;
    v[x].push_back(i);
    update(i, 1);
  }
  for (i = 1; i <= 100005; i++) {
    if (v[i].size() != 0) {
      curr = lower_bound(v[i].begin(), v[i].end(), pos) - v[i].begin();
      if (curr == v[i].size()) {
        curr = 0;
      }
      for (j = 0; j < v[i].size(); j++) {
        if (pos < v[i][curr]) {
          temp = answer(v[i][curr]) - answer(pos);
        } else {
          temp = answer(n) - answer(pos) + answer(v[i][curr]);
        }
        update(v[i][curr], -1);
        pos = v[i][curr];
        curr = (curr + 1) % v[i].size();
        ans += temp;
      }
    }
  }
  cout << ans;
  return 0;
}
