#include <bits/stdc++.h>
using namespace std;
int n, a[100002 + 10];
long long int cnt[100002 + 10], sum[100002 + 10], ans, tmp, tot;
vector<int> vt[100002 + 10];
void update(long long int tree[], int pos, int val) {
  for (; pos <= 100002; pos += pos & -pos) tree[pos] += val;
}
long long int query(long long int tree[], int pos) {
  long long int sum = 0;
  for (; pos > 0; pos -= pos & -pos) sum += tree[pos];
  return sum;
}
int binup(int pos, int num, int hi) {
  int lo = 0, mid, tmp = -1;
  while (lo <= hi) {
    mid = (lo + hi) >> 1;
    if (vt[pos][mid] >= num)
      tmp = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  return tmp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    vt[x].push_back(i);
    update(cnt, i, 1);
  }
  long long int tmp;
  for (int i = 1, prev = 0, siz, lol, j; i <= 100002; i++) {
    siz = vt[i].size();
    if (siz == 0) continue;
    sort(vt[i].begin(), vt[i].end());
    lol = binup(i, prev, siz - 1);
    if (lol == -1) lol = 0;
    j = lol;
    while (true) {
      tot = 0, tmp = 0;
      if (vt[i][j] > prev) {
        tmp = query(sum, vt[i][j]);
        tmp += vt[i][j];
        ans += tmp;
        update(cnt, vt[i][j], -1);
        update(sum, vt[i][j], -tmp);
        if (prev > 0) {
          update(sum, 1, -tmp);
          update(sum, prev, tmp);
        }
        tot = query(cnt, n) - query(cnt, vt[i][j]) + query(cnt, prev);
        update(sum, prev + 1, tot);
        update(sum, vt[i][j], -tot);
      } else {
        tmp = query(sum, vt[i][j]);
        tmp += vt[i][j];
        ans += tmp;
        update(cnt, vt[i][j], -1);
        update(sum, vt[i][j], -tmp);
        update(sum, prev, +tmp);
        tot = query(cnt, prev) - query(cnt, vt[i][j]);
        if (prev + 1 <= n) update(sum, prev + 1, tot);
        update(sum, 1, tot);
        update(sum, vt[i][j], -tot);
      }
      prev = vt[i][j];
      j++;
      if (j >= siz) j = 0;
      if (j == lol) break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
