#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 17;
const int INF = (int)1e8;
struct Node {
  int l, r;
  pair<int, int> val;
  Node() : l(-1), r(-1), val(INF, INF) {}
  Node(int _l, int _r) : l(_l), r(_r), val(make_pair(INF, _l)) {}
};
Node tree[2 * N];
int fenv[N];
void fenvAdd(int p, int d) {
  for (; p < N; p |= p + 1) fenv[p] += d;
}
int fenvGet(int r) {
  int res = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) res += fenv[r];
  return res;
}
void buildTree() {
  for (int i = 0; i < N; i++) tree[N + i] = Node(i, i + 1);
  for (int i = N - 1; i > 0; i--)
    tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r);
}
void treeSet(int v, int val) {
  v += N;
  tree[v].val.first = val;
  while (v > 1) {
    v >>= 1;
    tree[v].val = min(tree[2 * v].val, tree[2 * v + 1].val);
  }
}
pair<int, int> treeGet(int v, int l, int r) {
  if (l <= tree[v].l && tree[v].r <= r) return tree[v].val;
  if (l >= tree[v].r || tree[v].l >= r) return make_pair(INF, INF);
  return min(treeGet(2 * v, l, r), treeGet(2 * v + 1, l, r));
}
int n;
int a[N];
int main() {
  buildTree();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    treeSet(i, a[i]);
  }
  for (int i = 0; i < n; i++) fenvAdd(i, 1);
  sort(a, a + n);
  long long ans = 0;
  int pos = 0;
  for (int i = 0; i < n; i++) {
    pair<int, int> m = treeGet(1, pos, n);
    if (m.first != a[i]) {
      ans += n - i - fenvGet(pos - 1);
      pos = 0;
      m = treeGet(1, pos, n);
    }
    if (m.first != a[i]) throw;
    if (m.second < pos) throw;
    ans += fenvGet(m.second) - fenvGet(pos - 1);
    pos = m.second;
    treeSet(pos, INF);
    fenvAdd(pos, -1);
  }
  printf("%lld\n", ans);
  return 0;
}
