#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const int M = 100000;
class FenwickTree {
  int arr[N + 1];

 public:
  FenwickTree() = default;
  void add(int, int);
  int get_sum(int);
  int get_sum(int, int);
};
void FenwickTree::add(int i, int value) {
  for (; i <= N; i += (i & -i)) {
    this->arr[i] += value;
  }
}
int FenwickTree::get_sum(int i) {
  int ret = 0;
  for (; i > 0; i -= (i & -i)) {
    ret += this->arr[i];
  }
  return ret;
}
int FenwickTree::get_sum(int l, int r) {
  int ret = 0;
  if (l <= r) {
    ret = this->get_sum(r) - this->get_sum(l - 1);
  } else {
    ret = this->get_sum(l, N) + this->get_sum(r);
  }
  return ret;
}
set<int> idxs[N + 1];
FenwickTree bitree;
int main(void) {
  int i;
  int n;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    idxs[x].insert(i);
  }
  for (i = 1; i <= n; ++i) {
    bitree.add(i, 1);
  }
  int cur = 1;
  long long ans = 0LL;
  for (i = 1; i <= M; ++i) {
    while (!idxs[i].empty()) {
      auto p = idxs[i].lower_bound(cur);
      if (p == idxs[i].end()) {
        p = idxs[i].begin();
      }
      ans += bitree.get_sum(cur, *p);
      bitree.add(*p, -1);
      cur = *p;
      idxs[i].erase(p);
    }
  }
  printf("%lld\n", ans);
  exit(EXIT_SUCCESS);
}
