#include <bits/stdc++.h>
using namespace std;
vector<int> ind[100001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, t, curr = 0, size, cnt = 0;
  long long res = 0;
  cin >> n;
  size = n;
  int a[n], first[100001], last[100001], c[100001];
  set<int> s;
  memset(first, -1, sizeof first);
  memset(c, 0, sizeof c);
  vector<int>::iterator it;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ind[a[i]].push_back(i);
    c[a[i]]++;
    s.insert(a[i]);
    if (first[a[i]] == -1) first[a[i]] = i;
    last[a[i]] = i;
  }
  while (!s.empty()) {
    t = *s.begin();
    if (first[t] < curr) {
      res += size;
      it = lower_bound(ind[t].begin(), ind[t].end(), curr);
      cnt += ind[t].end() - it;
      it--;
      curr = *it;
      size -= cnt;
      cnt = it - ind[t].begin() + 1;
    } else {
      curr = last[t];
      cnt += c[t];
    }
    s.erase(s.begin());
  }
  res += cnt;
  cout << res;
  return 0;
}
