#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int a[N], b[N];
vector<int> v[N];
int c[N];
void modify(int x, int num) {
  while (x <= 100000) c[x] += num, x += x & -x;
}
int sum(int x) {
  int s = 0;
  while (x) s += c[x], x -= x & -x;
  return s;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) v[a[i]].push_back(i);
  long long ans = 0;
  int now = 0;
  for (int i = 1; i <= n;) {
    int p = lower_bound(v[b[i]].begin(), v[b[i]].end(), now) - v[b[i]].begin();
    if (p == 0) {
      int pos = v[b[i]][v[b[i]].size() - 1];
      ans += pos - sum(pos) - (now - sum(now));
      now = pos;
    } else {
      int pos = v[b[i]][p - 1];
      ans += n - sum(n) - (now - sum(now)) + pos - sum(pos);
      now = pos;
    }
    for (int j = 0; j < v[b[i]].size(); j++) modify(v[b[i]][j], 1);
    i += v[b[i]].size();
  }
  cout << ans << endl;
}
