#include <bits/stdc++.h>
using namespace std;
struct FenwickTree {
  vector<long long> bit;
  long long n;
  FenwickTree(long long n) {
    this->n = n;
    bit.assign(n, 0);
  }
  FenwickTree(vector<long long> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
  long long sum(long long r) {
    long long ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }
  long long sum(long long l, long long r) { return sum(r) - sum(l - 1); }
  void add(long long idx, long long delta) {
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
};
FenwickTree ft(100001);
set<long long> positions[100001];
priority_queue<long long, vector<long long>, greater<long long>> pq;
vector<long long> arr(100001);
long long n;
long long getsum(long long left, long long right) {
  if (right < left) {
    return ft.sum(n) - ft.sum(left - 1) + ft.sum(right);
  }
  return ft.sum(right) - ft.sum(left - 1);
}
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    positions[arr[i]].insert(i);
    ft.add(i, 1);
    pq.push(arr[i]);
  }
  long long pos = 1;
  long long ans = 0;
  while (!pq.empty()) {
    long long t = pq.top();
    pq.pop();
    auto it = positions[t].lower_bound(pos);
    if (it == positions[t].end()) {
      it = positions[t].begin();
    }
    long long idx = *it;
    long long cards = getsum(pos, idx);
    ans += cards;
    ft.add(idx, -1);
    pos = idx;
    positions[t].erase(it);
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
}
