#include <bits/stdc++.h>
using namespace std;
struct Node {
  int w, f;
} a[100010];
int i, j, k, p, n, m, c[100010 << 2], Last;
long long Ans;
inline bool Cmp(Node a, Node b) {
  if (a.w == b.w) return a.f < b.f;
  return a.w < b.w;
}
inline void Build(int x, int l, int r) {
  c[x] = r - l + 1;
  if (l == r) return;
  int Mid = l + r >> 1;
  Build(x << 1, l, Mid);
  Build(x << 1 | 1, Mid + 1, r);
}
inline int Query(int x, int l, int r, int L, int R) {
  if (l > R || r < L) return 0;
  if (l >= L && r <= R) return c[x];
  int Mid = l + r >> 1;
  return Query(x << 1, l, Mid, L, R) + Query(x << 1 | 1, Mid + 1, r, L, R);
}
inline void Update(int x, int l, int r, int y) {
  c[x]--;
  if (l == r) return;
  int Mid = l + r >> 1;
  if (Mid >= y)
    Update(x << 1, l, Mid, y);
  else
    Update(x << 1 | 1, Mid + 1, r, y);
}
inline int Get(int x, int y) {
  if (x <= y) return Query(1, 1, n, x, y);
  return Query(1, 1, n, 1, y) + Query(1, 1, n, x, n);
}
inline void Calc(int y) {
  Ans += Get(Last, y);
  Update(1, 1, n, y);
  Last = y;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i].w), a[i].f = i;
  sort(a + 1, a + n + 1, Cmp);
  Build(1, 1, n);
  for (i = 1; i <= n; i = j + 1) {
    k = 0;
    for (j = i; j < n && a[j + 1].w == a[j].w; j++)
      if (a[j].f > Last && !k) k = j;
    if (a[j].f > Last && !k) k = j;
    if (k) {
      for (p = k; p <= j; p++) Calc(a[p].f);
      for (p = i; p < k; p++) Calc(a[p].f);
    } else
      for (p = i; p <= j; p++) Calc(a[p].f);
  }
  printf("%I64d\n", Ans);
  return 0;
}
