#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int MAXN = 1000006;
int in[MAXN];
int r[MAXN];
int n;
int qq;
bool compare2(pair<int, int> p1, pair<int, int> p2) {
  int a = p1.second, b = p2.second;
  if (a > qq) {
    if (b > qq) {
      return a < b;
    } else {
      return true;
    }
  } else {
    if (b > qq) {
      return false;
    } else {
      return a < b;
    }
  }
}
pair<int, int> mvec[MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> in[i];
  for (int i = 0; i < n; i++) {
    mvec[i] = make_pair(in[i], i);
  }
  sort(mvec, mvec + n);
  for (int i = 0; i < n; i++) {
    int j = i;
    if (i == 0) {
      qq = -1;
    } else {
      qq = mvec[i - 1].second;
    }
    while (j < n && mvec[j].first == mvec[i].first) j++;
    sort(mvec + i, mvec + j, compare2);
    i = j - 1;
  }
  r[0] = 1;
  int pos = -1, val = 1, val2 = 1;
  for (int i = 1; i < n; i++) {
    r[i] = r[i - 1];
    if (mvec[i - 1].second > mvec[i].second) {
      r[i]++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += r[i];
  }
  cout << ans << endl;
}
