#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5;
int a[MAXN], tree[MAXN], n;
void del(int pos) {
  while (pos <= n) {
    tree[pos] += 1;
    pos += pos & -pos;
  }
}
int query(int pos) {
  int ret = 0;
  while (pos) {
    ret += tree[pos];
    pos -= pos & -pos;
  }
  return ret;
}
vector<int> v[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  int preloc = 0;
  long long ans = 0;
  for (int i = 1; i <= 100000; i++) {
    int pos = upper_bound(v[i].begin(), v[i].end(), preloc) - v[i].begin();
    if (v[i].size()) {
      if (pos > 0) {
        ans += n - preloc - (query(n) - query(preloc));
        ans += v[i][pos - 1] - query(v[i][pos - 1]);
        preloc = v[i][pos - 1];
      } else {
        ans += v[i].back() - preloc - query(v[i].back()) + query(preloc);
        preloc = v[i].back();
      }
      for (int j = 0; j < v[i].size(); j++) {
        del(v[i][j]);
      }
    }
  }
  printf("%lld\n", ans);
}
