#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> pos[N];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  set<int> numb;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    numb.insert(k);
    pos[k].push_back(i);
  }
  int n1 = n, amo = 0, last = 0;
  long long ans = 0;
  for (auto x : numb) {
    int d = 0;
    while (d < (int)pos[x].size() && pos[x][d] < last) {
      d++;
    }
    if (d == 0) {
      amo += pos[x].size();
      last = pos[x][pos[x].size() - 1];
    } else if (d == (int)pos[x].size()) {
      ans += n1;
      n1 -= amo;
      amo = pos[x].size();
      last = pos[x][pos[x].size() - 1];
    } else {
      amo += pos[x].size() - d;
      ans += n1;
      n1 -= amo;
      last = pos[x][d - 1];
      amo = d;
    }
  }
  ans += n1;
  cout << ans << "\n";
  return 0;
}
