#include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
int bit[100001];
vector<int> poss[100001];
void update(int idx, int val) {
  while (idx < 100001) {
    bit[idx] += val;
    idx += (idx & (-idx));
  }
}
int query(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += bit[idx];
    idx -= (idx & (-idx));
  }
  return sum;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    poss[a[i]].push_back(i);
    update(i, 1);
  }
  int curr = 0;
  long long int ans = 0;
  for (int i = 1; i < 100001; i++) {
    if (poss[i].empty()) continue;
    int ind =
        lower_bound(poss[i].begin(), poss[i].end(), curr) - poss[i].begin();
    if (ind == 0) {
      ans += (query(poss[i].back()) - query(curr));
      curr = poss[i].back();
    } else {
      ans += (query(n) - query(curr));
      ind--;
      ans += query(poss[i][ind]);
      curr = poss[i][ind];
    }
    for (auto it : poss[i]) update(it, -1);
  }
  printf("%lld", ans);
  return 0;
}
