#include <bits/stdc++.h>
int N, a[100001], p[100001], q[100001], T[100001];
long long O;
int pb(int s, int l, int r) {
  int sp = std::lower_bound(p + l, p + r + 1, s) - p;
  for (int i = l; i <= r; i++) {
    if (sp == r + 1) sp = l;
    q[i] = p[sp++];
  }
  return q[r];
}
void P(int x) {
  while (x <= N) T[x]--, x += x & -x;
}
int G(int x) {
  int r = 0;
  while (x) r += T[x], x &= x - 1;
  return r;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", a + i);
    p[i] = i;
  }
  std::sort(p + 1, p + N + 1, [](int x, int y) {
    return a[x] < a[y] || (!(a[y] < a[x]) && x < y);
  });
  for (int i = 1, s = 1, j; i <= N; i = j) {
    j = i + 1;
    while (j <= N && a[p[i]] == a[p[j]]) j++;
    s = pb(s, i, j - 1);
  }
  for (int i = 1; i <= N; i++) T[i] = i & -i;
  for (int i = 1; i <= N; i++) {
    O += G(q[i]) - G(q[i - 1]);
    if (q[i - 1] > q[i]) O += N - i + 1;
    P(q[i]);
  }
  printf("%lld\n", O);
  return 0;
}
