#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int sum[100005 << 2], n;
int a[100005];
void add(int l, int r, int index, int now, int count) {
  if (l == r) {
    sum[now] += count;
    return;
  }
  int mid = l + r >> 1;
  if (index <= mid)
    add(l, mid, index, now << 1, count);
  else
    add(mid + 1, r, index, now << 1 | 1, count);
  sum[now] = sum[now << 1] + sum[now << 1 | 1];
}
int query(int l1, int r1, int l2, int r2, int now) {
  if (l2 >= n || r2 < 0) return 0;
  if (l2 <= l1 && r2 >= r1) return sum[now];
  int mid = l1 + r1 >> 1;
  int a = 0, b = 0;
  if (l2 <= mid) a = query(l1, mid, l2, r2, now << 1);
  if (r2 > mid) b = query(mid + 1, r1, l2, r2, now << 1 | 1);
  return a + b;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  sort(a, a + n);
  int now = -1;
  long long ans = 0;
  for (int i = 0; i < n;) {
    int pos =
        lower_bound(v[a[i]].begin(), v[a[i]].end(), now) - v[a[i]].begin();
    int size = v[a[i]].size();
    if (pos) {
      pos = v[a[i]][pos - 1];
      ans += n - now - 1 - query(0, n - 1, now + 1, n - 1, 1) + pos + 1 -
             query(0, n - 1, 0, pos, 1);
    } else {
      pos = v[a[i]][size - 1];
      ans += pos + 1 - query(0, n - 1, 0, pos, 1) -
             (now + 1 - query(0, n - 1, 0, now, 1));
    }
    now = pos;
    for (int j = 0; j < size; j++) add(0, n - 1, v[a[i]][j], 1, 1);
    i += size;
  }
  printf("%lld\n", ans);
}
