#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 17;
int N, maxv;
long long R;
vector<int> v[maxn];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int A;
    scanf("%d", &A);
    v[A].push_back(i);
    maxv = max(maxv, A);
  }
  int x = 0, count = 1;
  for (int i = 1; i <= maxv; i++)
    if (!v[i].empty()) {
      vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), x);
      R += 1LL * count * (v[i].end() - it);
      x = v[i].back();
      v[i].erase(it, v[i].end());
      if (!v[i].empty()) count++, i--, x = 0;
    }
  printf("%I64d\n", R);
  return 0;
}
