#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, F[maxn];
vector<int> v[maxn];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v[a].push_back(i);
  }
  long long ans = 0, pos = -1, k = 1, t = 0;
  for (int i = 1; i < maxn; i++) {
    if (v[i].size()) {
      if (v[i][0] > pos) {
        for (int j = 0; j < v[i].size(); j++) {
          F[t++] = k;
        }
        pos = v[i][v[i].size() - 1];
      } else {
        int ind = 0, j = 0;
        for (j = 0; j < v[i].size() && v[i][j] < pos; j++) {
          ind = v[i][j];
          F[t++] = k + 1;
        }
        for (; j < v[i].size(); j++) {
          F[t++] = k;
        }
        pos = ind;
        k++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    ans += F[i];
  }
  cout << ans << endl;
}
