#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
struct data {
  long long v, p;
} a[400010];
long long sum[400010], last, ans, n, c[400010];
long long get(long long x, long long y) { return (y + n - x) % n; }
bool cmp(data a, data b) { return a.v < b.v; }
bool cmp1(data a, data b) { return get(last, a.p) < get(last, b.p); }
void add(long long x, long long v) {
  for (; x <= n; x += x & -x) c[x] += v;
}
long long ask(long long x) {
  long long ans = 0;
  for (; x; x -= x & -x) ans += c[x];
  return ans;
}
long long Q(long long x, long long y) {
  return sum[y] - sum[x - 1] + ask(y) - ask(x - 1);
}
long long calc(long long x, long long y) {
  if (x <= y)
    return Q(x, y);
  else
    return Q(x, n) + Q(1, y);
}
int main() {
  n = read();
  for (long long i = 1; i <= n; ++i) a[i].v = read(), a[i].p = i;
  sort(a + 1, a + n + 1, cmp);
  last = 1;
  for (long long i = 1; i <= n; ++i) sum[i] = i;
  for (long long i = 1, j; i <= n; i = j + 1) {
    j = i;
    while (a[j + 1].v == a[i].v && j < n) ++j;
    long long cho = i;
    sort(a + i, a + j + 1, cmp1);
    for (long long k = i; k <= j; ++k) {
      ans += calc(last, a[k].p);
      last = a[k].p;
      add(a[k].p, -1);
    }
  }
  writeln(ans);
}
