#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n, arr[maxn], C[maxn], brr[maxn];
set<int> mp[maxn];
void add(int x, int dx) {
  while (x <= n) {
    C[x] += dx;
    x += x & (-x);
  }
}
int sum(int x) {
  int ret = 0;
  while (x > 0) {
    ret += C[x];
    x -= x & (-x);
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", arr + i);
    brr[i] = arr[i];
    add(i, 1);
    mp[arr[i]].insert(i);
  }
  sort(brr + 1, brr + n + 1);
  int pos = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int val = brr[i];
    set<int>::iterator it = mp[val].upper_bound(pos);
    if (it != mp[val].end()) {
      ans += sum(*it) - sum(pos);
    } else {
      it = mp[val].begin();
      ans += sum(n) - sum(pos) + sum(*it);
    }
    add(*it, -1);
    pos = *it;
    mp[val].erase(it);
  }
  cout << ans << endl;
  return 0;
}
