#include <bits/stdc++.h>
using namespace std;
int A[110000];
vector<int> x[110000];
const int N = 100000;
int n;
void add(int i, int x) {
  while (i <= n) {
    A[i] += x;
    i += (i & (-i));
  }
}
int sum(int i) {
  int ans = 0;
  while (i > 0) {
    ans += A[i];
    i -= (i & (-i));
  }
  return ans;
}
int cal(int l, int r) {
  if (l > r) return cal(l, n) + cal(0, r);
  return sum(r) - sum(l);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    add(i, 1);
    int k;
    scanf("%d", &k);
    x[k].push_back(i);
  }
  int now = 0;
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    if (x[i].size() == 0) continue;
    int pos = lower_bound(x[i].begin(), x[i].end(), now) - x[i].begin();
    if (pos == x[i].size()) pos = 0;
    for (int j = 0; j < x[i].size(); j++) {
      ans += cal(now, x[i][pos]);
      add(x[i][pos], -1);
      now = x[i][pos];
      pos = (pos + 1) % (int)(x[i].size());
    }
  }
  cout << ans << endl;
  return 0;
}
