#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, bit[maxn], key[maxn];
long long ans;
struct node {
  int x, y;
} a[maxn];
set<int> g[maxn];
int b[maxn];
set<int>::iterator it;
void add(int x, int y) {
  for (; x <= n; x += x & -x) bit[x] += y;
}
int ask(int x) {
  int rt = 0;
  for (; x; x -= x & -x) rt += bit[x];
  return rt;
}
int calc(int x, int y) {
  if (x > y) return 0;
  return ask(y) - ask(x - 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i].x), g[a[i].x].insert(i), b[i] = a[i].x;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i) add(i, 1);
  int now, las;
  ans = calc(1, (*g[b[1]].begin()));
  add((*g[b[1]].begin()), -1);
  las = (*g[b[1]].begin());
  g[b[1]].erase(g[b[1]].begin());
  for (int i = 2; i <= n; ++i) {
    it = g[b[i]].upper_bound(las);
    if (it == g[b[i]].end()) it = g[b[i]].begin();
    now = (*it);
    if (now > las)
      ans += calc(las, now);
    else
      ans += calc(las, n) + calc(1, now);
    add(now, -1);
    it = g[b[i]].upper_bound(las);
    if (it == g[b[i]].end())
      g[b[i]].erase(g[b[i]].begin());
    else
      g[b[i]].erase(g[b[i]].upper_bound(las));
    las = now;
  }
  printf("%lld\n", ans);
  return 0;
}
