#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 10;
int n, a[maxN];
vector<int> b[maxN];
int fen[maxN];
int get(int l, int r) {
  int res = 0;
  for (int k = r; k > 0; k -= k & -k) res += fen[k];
  for (int k = l; k > 0; k -= k & -k) res -= fen[k];
  return res;
}
void add(int x, int v) {
  for (int k = x + 1; k < maxN; k += k & -k) fen[k] += v;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], b[a[i]].push_back(i);
  for (int i = 0; i < n; i++) add(i, 1);
  long long ptr = 0, sum = 0;
  for (int i = 1; i < maxN; i++) {
    int k = 0, l = (int)b[i].size();
    while (k < l && ptr > b[i][k]) k++;
    if (k == l) k = 0;
    for (int o = 0; o < l; o++, k = (k + 1) % l) {
      if (ptr <= b[i][k])
        sum += get(ptr, b[i][k]);
      else
        sum += get(ptr, n), sum += get(0, b[i][k]);
      ptr = b[i][k];
      add(b[i][k], -1);
      sum++;
    }
  }
  cout << sum << endl;
  return 0;
}
