#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100055];
void reorg(int l, int r, int m) {
  int k = 0;
  while (a[l + k].second < m && k + l < r) k++;
  vector<int> v((r - l + 1) * 2);
  if (a[l + k].second > m) {
    for (int i = 0; i < r - l + 1; i++) v[i] = a[i + l].second;
    for (int i = 0; i < r - l + 1; i++) v[i + r - l + 1] = a[i + l].second;
    for (int i = 0; i < r - l + 1; i++) a[l + i].second = v[i + k];
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[i] = {x, i};
  }
  sort(a, a + n);
  int last = -1, j = -1;
  a[n].second = a[n - 1].second + 1;
  for (int i = 1; i < n + 1; i++) {
    if (a[i].first != a[i - 1].first) {
      if (j != -1) reorg(j, i - 1, last);
      j = i;
      last = a[i - 1].second;
    }
  }
  long long ans = n, temp = n, k = 1;
  for (int i = 1; i < n; i++) {
    if (a[i].second > a[i - 1].second)
      k++;
    else {
      temp -= k;
      ans += temp;
      k = 1;
    }
  }
  cout << ans;
  return 0;
}
