#include <bits/stdc++.h>
using namespace std;
int st[4 * 100005];
int n, a[100005];
long long ans = 0;
int cur = ((int)1e9), ptr = 0;
vector<vector<int> > c(100005);
void build(int p, int l, int r) {
  if (l == r) {
    st[p] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  st[p] = a[st[p << 1]] <= a[st[p << 1 | 1]] ? st[p << 1] : st[p << 1 | 1];
}
int query(int p, int l, int r, int lq, int rq) {
  if (lq > r || rq < l || lq > rq) return n;
  if (lq <= l && r <= rq) return st[p];
  int mid = (l + r) >> 1;
  int lans = query(p << 1, l, mid, lq, rq);
  int rans = query(p << 1 | 1, mid + 1, r, lq, rq);
  return a[lans] <= a[rans] ? lans : rans;
}
void update(int p, int l, int r, int i, int val) {
  if (i > r || i < l) return;
  if (l == r) {
    st[p] = n;
    return;
  }
  int mid = (l + r) >> 1;
  update(p << 1, l, mid, i, val);
  update(p << 1 | 1, mid + 1, r, i, val);
  st[p] = a[st[p << 1]] <= a[st[p << 1 | 1]] ? st[p << 1] : st[p << 1 | 1];
}
int ft[100005] = {};
int ftquery(int i) {
  int ans = 0;
  i++;
  for (; i; i -= i & (-i)) ans += ft[i];
  return ans;
}
void ftupdate(int i) {
  i++;
  for (; i <= n; i += i & (-i)) ft[i]++;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int largest = 0;
  for (int i = 0, _i = (n); i < _i; ++i) {
    cin >> a[i];
    largest = max(largest, a[i]);
    cur = min(cur, a[i]);
    c[a[i]].push_back(i);
  }
  a[n] = ((int)1e9);
  build(1, 0, n - 1);
  while (cur <= largest) {
    if (((int)((c[cur]).size())) == 0) {
      cur++;
      continue;
    }
    int nxt = ptr;
    for (int card : c[cur]) {
      if ((ptr > card ? n - ptr + card : card - ptr) >
          (ptr > nxt ? n - ptr + nxt : nxt - ptr))
        nxt = card;
    }
    ans += (ptr > nxt ? n - ptr + nxt : nxt - ptr) -
           (nxt < ptr ? ftquery(n - 1) - ftquery(ptr - 1) + ftquery(nxt)
                      : ftquery(nxt) - ftquery(ptr - 1)) +
           1;
    for (int card : c[cur]) {
      update(1, 0, n - 1, card, ((int)1e9));
      ftupdate(card);
    }
    ptr = nxt;
    nxt = query(1, 0, n - 1, ptr + 1, n - 1);
    if (nxt == n) nxt = query(1, 0, n - 1, 0, ptr - 1);
    if (nxt != n)
      ans += (ptr > nxt ? n - ptr + nxt : nxt - ptr) -
             (nxt < ptr ? ftquery(n - 1) - ftquery(ptr - 1) + ftquery(nxt)
                        : ftquery(nxt) - ftquery(ptr - 1));
    cur++;
    ptr = nxt;
  }
  cout << ans;
}
