#include <bits/stdc++.h>
int n;
int a[(200010)], b[(200010)], c[(200010)];
std::pair<int, int> pr[(200010)];
void insert(int i, int d) {
  for (; i <= n + n; i += ((i) & (-(i)))) c[i] += d;
}
int getsum(int i) {
  int ret = 0;
  for (; i; i -= ((i) & (-(i)))) ret += c[i];
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    pr[i] = {a[i], i};
    insert(i, 1);
    insert(i + n, 1);
  }
  std::sort(pr + 1, n + pr + 1);
  for (int last = 0, wjj = 1, tt = 1, i = 1, j = 1; i <= n; i = j) {
    int min = 0;
    for (j = i; j <= n && pr[j].first == pr[i].first; ++j)
      ;
    std::vector<int> vec((j - i + 5) << 1);
    for (int p = i; p < j; ++p) {
      vec[tt] = pr[p].second;
      vec[tt + j - i] = pr[p].second;
      if (pr[p].second > last && (!min || vec[min] > vec[tt])) {
        min = tt;
      }
      ++tt;
    }
    if (!min) min = 1;
    for (int p = min; p < min + j - i; ++p) {
      b[wjj++] = last = vec[p];
    }
    tt = 1;
  }
  long long ans = getsum(b[1]);
  insert(b[1], -1);
  insert(b[1] + n, -1);
  for (int i = 2; i <= n; ++i) {
    int l = b[i - 1], r = b[i];
    if (l >= r) r += n;
    ans += getsum(r) - getsum(l);
    insert(r, -1);
    if (r > n)
      insert(r - n, -1);
    else
      insert(r + n, -1);
  }
  printf("%I64d\n", ans);
  return 0;
}
