#include <bits/stdc++.h>
using namespace std;
const int maxn = 100020;
int num[maxn];
int c[maxn];
vector<int> g[maxn];
int lowbit(int x) { return x & (-x); }
void add(int pos, int val) {
  while (pos < maxn) {
    c[pos] += val;
    pos += lowbit(pos);
  }
}
int query(int pos) {
  if (pos == 0) return 0;
  int ret = 0;
  while (pos > 0) {
    ret += c[pos];
    pos -= lowbit(pos);
  }
  return ret;
}
int query(int l, int r) { return query(r) - query(l - 1); }
int find_pos(int last_pos, int i) {
  int ret = 0;
  for (int p = g[i].size() - 1; p >= 0; p--) {
    if (g[i][p] >= last_pos) ret = p;
  }
  return ret;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < maxn; i++) g[i].clear();
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      g[x].push_back(i);
      add(i, 1);
    }
    long long ans = 0;
    int last_pos = 1;
    for (int i = 0; i < maxn; i++) {
      if (g[i].size() == 0) continue;
      int memo = find_pos(last_pos, i);
      for (int j = memo;;) {
        int pos = g[i][j];
        if (pos >= last_pos)
          ans += query(last_pos, pos);
        else
          ans += query(last_pos, maxn - 1) + query(1, pos);
        add(pos, -1);
        last_pos = pos;
        j = (j + 1) % g[i].size();
        if (j == memo) break;
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
