#include <bits/stdc++.h>
using namespace std;
vector<int> S[100005];
int n, x;
long long ans;
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    S[x].push_back(i);
  }
  int currpos = 0, nextpos = 0, round_no = 1;
  for (int i = 1; i <= 100000; i++) {
    for (int y : S[i]) {
      if (y > currpos) {
        ans += round_no;
        nextpos = max(nextpos, y);
      } else {
        nextpos = max(nextpos, y + n);
        ans += round_no + 1;
      }
    }
    if (nextpos > n) {
      nextpos -= n;
      round_no++;
    }
    currpos = nextpos;
  }
  cout << ans << endl;
}
