#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int N = 100000 + 5;
const int K = 50 + 5;
int n, tree[N];
vector<int> a[N];
void add(int x, int v) {
  for (; x <= n; x += x & (-x)) tree[x] += v;
}
int sum(int x) {
  int v = 0;
  for (; x; x -= x & (-x)) v += tree[x];
  return v;
}
int cal(int l, int r) {
  if (l > r) return cal(l, n) + cal(0, r);
  return sum(r) - sum(l);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    add(i, 1);
    a[x].push_back(i);
  }
  int pre = 0;
  long long ans = 0;
  for (int i = 1; i < N; ++i) {
    int now = lower_bound(a[i].begin(), a[i].end(), pre) - a[i].begin();
    if (now == a[i].size()) now = 0;
    for (int j = 0; j < a[i].size(); ++j) {
      ans += cal(pre, a[i][now]);
      add(a[i][now], -1);
      pre = a[i][now];
      now = (now + 1) % a[i].size();
    }
  }
  printf("%lld\n", ans);
}
