#include <bits/stdc++.h>
using namespace std;
long long IT[400005];
void build(int id, int first, int second) {
  if (first == second) {
    IT[id] = 1;
    return;
  }
  IT[id] = second - first + 1;
  int mid = (first + second) / 2;
  build(id * 2, first, mid);
  build(id * 2 + 1, mid + 1, second);
}
void update(int id, int first, int second, int p) {
  if (p < first || second < p) return;
  if (first == second) {
    IT[id] = 0;
    return;
  }
  IT[id]--;
  int mid = (first + second) / 2;
  update(id * 2, first, mid, p);
  update(id * 2 + 1, mid + 1, second, p);
}
long long get(int id, int first, int second, int u, int v) {
  if (v < first || second < u) return 0;
  if (u <= first && second <= v) return IT[id];
  int mid = (first + second) / 2;
  return get(id * 2, first, mid, u, v) + get(id * 2 + 1, mid + 1, second, u, v);
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > f;
  build(1, 0, n);
  for (int i = 1; i <= n; i++) {
    int bf;
    cin >> bf;
    f.push_back(make_pair(bf, i));
  }
  f.push_back(make_pair(0, 0));
  sort(f.begin(), f.end());
  long long ans = 0;
  int current = f[1].first;
  int p1 = 0;
  int i = 1;
  while (i < f.size()) {
    int current = f[i].first;
    deque<int> q;
    while (f[i].first == current && f[i].second < p1) {
      q.push_back(f[i].second);
      i++;
    }
    if (f[i].first == current) {
      int bf = i;
      while (i < f.size() && f[i].first == current) i++;
      for (int j = i - 1; j >= bf; j--) q.push_front(f[j].second);
    }
    while (!q.empty()) {
      int p2 = q.front();
      if (p2 > p1) {
        ans += get(1, 0, n, p1, p2) - 1;
        update(1, 0, n, p1);
      } else {
        ans += get(1, 0, n, p1, n) + get(1, 0, n, 1, p2) - 1;
        update(1, 0, n, p1);
      }
      q.pop_front();
      p1 = p2;
    }
  }
  cout << ans;
  return 0;
}
