#include <bits/stdc++.h>
int ft[100005] = {};
std::vector<std::vector<int> > pos;
void upd(int x, int v) {
  while (x < 100005) {
    ft[x] += v;
    x += (x & -x);
  }
}
int cul(int x) {
  int sum = 0;
  while (x > 0) {
    sum += ft[x];
    x -= (x & -x);
  }
  return sum;
}
int rsq(int l, int r, int n) {
  if (l <= r)
    return cul(r) - cul(l - 1);
  else
    return cul(n) - cul(l - 1) + cul(r);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  pos.resize(100005);
  int n, curr_pos, x;
  long long int ans = 0;
  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    std::cin >> x;
    pos[x].push_back(i);
    upd(i, 1);
  }
  curr_pos = 1;
  for (int i = 1; i <= 100005; i++) {
    if (!pos[i].empty()) {
      std::vector<int>::iterator it =
          std::lower_bound(pos[i].begin(), pos[i].end(), curr_pos);
      if (it == pos[i].end()) it = pos[i].begin();
      for (int checked = 0; checked < pos[i].size(); checked++) {
        ans += rsq(curr_pos, *it, n);
        upd(*it, -1);
        curr_pos = *it;
        it++;
        if (it == pos[i].end()) it = pos[i].begin();
      }
    }
  }
  std::cout << ans << '\n';
}
