#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = 100001;
long long int n;
int bit[N], vis[N];
void add(int idx, int u) {
  idx++;
  while (idx < N) {
    bit[idx] += u;
    idx += idx & -idx;
  }
}
int query(int p) {
  if (p < 0) return 0;
  p++;
  int ret = 0;
  while (p > 0) {
    ret += bit[p];
    p -= p & -p;
  }
  return ret;
}
int query(int a, int b) {
  if (b < a) return query(n) - query(a - 1) + query(b);
  return query(b) - query(a - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) add(i, 1);
  priority_queue<pair<int, int> > pq;
  for (long long int i = 0; i < n; i++) {
    long long int a;
    cin >> a;
    pq.push({-a, -i});
  }
  long long int cl = 0;
  int cur = 0;
  while (!pq.empty()) {
    vector<int> nexv;
    int tval = pq.top().first;
    while (!pq.empty() && pq.top().first == tval)
      nexv.push_back(-pq.top().second), pq.pop();
    auto it = lower_bound(nexv.begin(), nexv.end(), cur);
    auto jt = it;
    do {
      if (jt == nexv.end()) jt = nexv.begin();
      int nex = *jt;
      if (vis[nex]) break;
      vis[nex] = 1;
      long long int miss = query(cur, nex);
      add(nex, -1);
      cl += miss;
      jt++;
      cur = nex;
    } while (it != jt);
  }
  cout << cl << '\n';
  return 0;
}
