#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
const double ex = 1e-10;
using namespace std;
vector<int> E[100111];
int k[100111];
long long c[100111];
int gg[100111];
map<int, int> M;
int n;
int lowbit(int k) { return k & (-k); }
void Modify(int num, long long v) {
  while (num <= n) {
    c[num] += v;
    num += lowbit(num);
  }
}
long long Sum(int num) {
  long long ans = 0;
  while (num > 0) {
    ans += c[num];
    num -= lowbit(num);
  }
  return ans;
}
long long getans(int i, int j) {
  if (i < j)
    return -Sum(i) + Sum(j);
  else
    return Sum(n) - Sum(i) + Sum(j);
}
int main() {
  cin >> n;
  memset(c, 0, sizeof(c));
  for (int i = 1; i <= n; i++) Modify(i, 1);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    k[i] = a;
    if (M[a] == 0) {
      M[a] = ++cnt;
    }
    E[M[a]].push_back(i);
  }
  cnt = 0;
  sort(k + 1, k + n + 1);
  k[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (k[i] != k[i - 1]) gg[++cnt] = k[i];
  }
  int b = 0;
  long long ans = 0;
  for (int i = 1; i <= cnt; i++) {
    int id = M[gg[i]];
    int l = E[id].size();
    int t;
    t = lower_bound(E[id].begin(), E[id].end(), b) - E[id].begin();
    if (lower_bound(E[id].begin(), E[id].end(), b) == E[id].end()) t = 0;
    for (int j = 1; j <= l; j++) {
      ans += getans(b, E[id][t]);
      Modify(E[id][t], -1);
      b = E[id][t];
      t = (t + 1) % l;
    }
  }
  cout << ans << endl;
  return 0;
}
