#include <bits/stdc++.h>
using namespace std;
long long n, a[100005];
vector<long long> v[100005];
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i < n + 1; i++) {
    cin >> a[i];
    v[a[i]].push_back(i);
  }
  long long curr = 0, next = 0, roun = 1;
  long long ans = 0;
  for (long long i = 1; i < 100005; i++) {
    for (long long y : v[i]) {
      if (y > curr) {
        ans += roun;
        next = max(next, y);
      } else {
        ans += roun + 1;
        next = max(next, y + n);
      }
    }
    if (next > n) {
      next -= n;
      roun++;
    }
    curr = next;
  }
  cout << ans;
}
