#include <bits/stdc++.h>
using namespace std;
int arr[100009], tree[4 * 100009], fre[4 * 100009], p;
vector<int> v;
void create(int n, int st, int en) {
  if (st == en) {
    tree[n] = st;
    return;
  }
  int m = (st + en) / 2;
  create(n * 2, st, m);
  create(n * 2 + 1, m + 1, en);
  if (arr[tree[n * 2]] <= arr[tree[n * 2 + 1]])
    tree[n] = tree[n * 2];
  else
    tree[n] = tree[n * 2 + 1];
}
void upd(int n, int st, int en, int i) {
  if (st > i || en < i) return;
  if (st >= i && en <= i) {
    tree[n] = p;
    return;
  }
  int m = (st + en) / 2;
  upd(n * 2, st, m, i);
  upd(n * 2 + 1, m + 1, en, i);
  if (arr[tree[n * 2]] <= arr[tree[n * 2 + 1]])
    tree[n] = tree[n * 2];
  else
    tree[n] = tree[n * 2 + 1];
}
int query(int n, int st, int en, int i, int j) {
  if (st > j || en < i) return p;
  if (st >= i && en <= j) return tree[n];
  int m = (st + en) / 2;
  int a = query(n * 2, st, m, i, j);
  int b = query(n * 2 + 1, m + 1, en, i, j);
  if (arr[a] <= arr[b])
    return a;
  else
    return b;
}
void upd1(int n, int st, int en, int i) {
  if (st > i || en < i) return;
  if (st >= i && en <= i) {
    fre[n] = 1;
    return;
  }
  int m = (st + en) / 2;
  upd1(n * 2, st, m, i);
  upd1(n * 2 + 1, m + 1, en, i);
  fre[n] = fre[n * 2] + fre[n * 2 + 1];
}
int query1(int n, int st, int en, int i, int j) {
  if (st > j || en < i) return 0;
  if (st >= i && en <= j) return fre[n];
  int m = (st + en) / 2;
  return query1(n * 2, st, m, i, j) + query1(n * 2 + 1, m + 1, en, i, j);
}
int main() {
  int n, i, a, b, pos, k, cnt = 1;
  long long ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &arr[i]);
  arr[++n] = 100009;
  p = n;
  create(1, 1, n);
  upd1(1, 1, n, n);
  pos = 1;
  while (cnt < n) {
    cnt++;
    a = query(1, 1, n, 1, n);
    b = query(1, 1, n, pos, n);
    if (arr[a] == arr[b]) {
      k = b - pos + 1;
      k -= query1(1, 1, n, pos, b);
      ans += k;
      upd(1, 1, n, b);
      upd1(1, 1, n, b);
      arr[b] = 100009;
      pos = (b + 1) % n;
      if (!pos) pos = 1;
      continue;
    }
    k = n - pos + 1;
    k -= query1(1, 1, n, pos, n);
    ans += k;
    k = a;
    k -= query1(1, 1, n, 1, a);
    ans += k;
    upd(1, 1, n, a);
    upd1(1, 1, n, a);
    arr[a] = 100009;
    pos = (a + 1) % n;
    if (!pos) pos = 1;
  }
  printf("%I64d\n", ans);
  return 0;
}
