#include <bits/stdc++.h>
#pragma GCC optimize("03")
using namespace std;
long long int const mod = 1000000007;
long long int const N = 100010;
long long int bit[N + 10], ar[N];
set<long long int> pos[N + 10];
multiset<long long int> ss;
void update(long long int x, long long int val) {
  for (; x < N; x += x & -x) bit[x] += val;
}
long long int query(long long int x) {
  long long int sum = 0;
  for (; x > 0; x -= x & -x) sum += bit[x];
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, x, sum = 0, y, val, id, x1;
  cin >> n;
  for (long long int i = 1; i <= n; i++) {
    cin >> ar[i];
    update(i, 1);
    pos[ar[i]].insert(i);
    ss.insert(ar[i]);
  }
  x = 0;
  while (!ss.empty()) {
    val = *(ss.begin());
    ss.erase(ss.begin());
    auto it = pos[val].lower_bound(x);
    if (it != pos[val].end()) {
      x1 = *it;
      y = query(x1) - query(x);
      sum += y;
      x = x1;
      update(x1, -1);
      pos[val].erase(it);
    } else {
      x1 = *(pos[val].begin());
      y = query(n) - query(x);
      y += query(x1);
      sum += y;
      x = x1;
      update(x1, -1);
      pos[val].erase(pos[val].begin());
    }
  }
  cout << sum << "\n";
  return 0;
}
