#include <bits/stdc++.h>
using namespace std;
vector<long long> vec[1000100];
long long a[100010];
long long cnt[100100];
int main() {
  long long n, pre = 0, x = 0, cur, Size;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &x);
    vec[x].push_back(i);
  }
  x = 1;
  for (long long i = 1; i <= 100010; i++) {
    Size = vec[i].size();
    if (Size) {
      cur = lower_bound(vec[i].begin(), vec[i].end(), pre) - vec[i].begin();
      cnt[x] += Size - cur;
      pre = vec[i][Size - 1];
      if (cur) {
        x++;
        cnt[x] += cur;
        pre = vec[i][cur - 1];
      }
    }
  }
  long long ans = 0;
  cur = 1;
  while (n) {
    ans += n;
    n -= cnt[cur++];
  }
  cout << ans << endl;
  return 0;
}
