#include <bits/stdc++.h>
using namespace std;
const int Size = 1e5;
int main() {
  vector<int> v[Size];
  int n;
  scanf("%d", &n);
  int a;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    v[a - 1].push_back(i);
  }
  long long ans = 0;
  int kil = 1;
  int pos = 0;
  for (int i = 0; i < Size; ++i)
    if (v[i].size() > 0) {
      vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), pos);
      ans +=
          (long long)kil * (v[i].end() - it) + (kil + 1) * (it - v[i].begin());
      if (it == v[i].begin())
        pos = v[i].back();
      else {
        --it;
        pos = *it;
        ++kil;
      }
    }
  printf("%I64d\n", ans);
}
