#include <bits/stdc++.h>
using namespace std;
int tree[100010], a[100010], b[100010], n, k, key;
long long ans;
void modify(int pos, int v) {
  while (pos <= n) {
    tree[pos] += v;
    pos += (pos & (-pos));
  }
}
int query(int pos) {
  int ret = 0;
  while (pos) {
    ret += tree[pos];
    pos -= (pos & (-pos));
  }
  return ret;
}
bool comp1(const int& comp_a, const int& comp_b) {
  return a[comp_a] < a[comp_b];
}
bool comp2(const int& comp_a, const int& comp_b) {
  if (comp_a < key && comp_b < key) return comp_a < comp_b;
  if (comp_a > key && comp_b < key) return 1;
  if (comp_a < key && comp_b > key) return 0;
  return comp_a < comp_b;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) b[i] = i;
  for (int i = 1; i <= n; i++) modify(i, 1);
  sort(b + 1, b + 1 + n, comp1);
  int l = 1, r = 1;
  while (r <= n) {
    while (a[b[r]] == a[b[l]]) r++;
    sort(b + l, b + r, comp2);
    if (b[r - 1] < key)
      ans += query(b[r - 1]) + query(n) - query(key);
    else
      ans += query(b[r - 1]) - query(key);
    for (int i = l; i <= r - 1; i++) modify(b[i], -1);
    key = b[r - 1];
    l = r;
  }
  cout << ans << endl;
  return 0;
}
