#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
const long long infl = 10000000000000000;
const int maxn = 100000 + 10;
const int maxm = 1000000 + 10;
int in(int &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return -1;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
int bit[maxn];
int num[maxn];
set<int> s[maxn];
void update(int x, int v) {
  while (x < maxn) {
    bit[x] += v;
    x += (x & (-x));
  }
}
int query(int x) {
  int res = 0;
  while (x) {
    res += bit[x];
    x -= (x & (-x));
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &num[i]);
    s[num[i]].insert(i);
    update(i, 1);
  }
  int now = 1, mn = 0;
  int left = n;
  long long ans = 0;
  while (left) {
    while (s[mn].empty()) mn++;
    if (s[mn].lower_bound(now) != s[mn].end()) {
      int tmp = now;
      now = *s[mn].lower_bound(now);
      ans += 1LL * query(now) - query(tmp - 1);
    } else {
      int tmp = now;
      now = *s[mn].lower_bound(1);
      ans += 1LL * query(n) - query(tmp - 1) + query(now);
    }
    update(now, -1);
    s[mn].erase(now);
    --left;
  }
  printf("%lld\n", ans);
  return 0;
}
