#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v;
vector<set<int>> p(100009);
int t[400009];
long long anw;
int pos;
int sum(int v, int tl, int tr, int l, int r) {
  if (l > r) return 0;
  if (l == tl && r == tr) return t[v];
  int tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
int sum(int l, int r) { return sum(1, 0, n - 1, l, r); }
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr)
    t[v] = new_val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
void update(int pos, int new_val) { update(1, 0, n - 1, pos, new_val); }
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
    p[x].insert(i);
  }
  sort(v.begin(), v.end());
  for (int val : v) {
    auto lb = p[val].lower_bound(pos);
    if (lb == p[val].end()) {
      lb = p[val].begin();
    }
    int next_pos = *lb;
    p[val].erase(lb);
    if (next_pos >= pos) {
      anw += next_pos - pos + 1 - sum(pos, next_pos);
    } else {
      anw += n + (next_pos - pos) + 1 - sum(0, next_pos) - sum(pos, n - 1);
    }
    update(next_pos, 1);
    pos = (next_pos + 1) % n;
  }
  cout << anw << endl;
}
