#include <bits/stdc++.h>
using namespace std;
const int maxn = 1E5 + 77;
set<pair<int, int> > S;
int N, p;
long long ans = 0;
struct SEG {
  SEG *ch[2];
  int v;
  SEG() { memset(this, 0, sizeof(SEG)); }
  void maintain() { v = (ch[0] ? ch[0]->v : 0) + (ch[1] ? ch[1]->v : 0); }
} * root;
void segSetVal(SEG *&tr, int L, int R, int p) {
  if (!tr) tr = new SEG;
  if (L == R) {
    tr->v = 1;
    return;
  }
  int M = L + ((R - L) >> 1);
  if (p <= M)
    segSetVal(tr->ch[0], L, M, p);
  else
    segSetVal(tr->ch[1], M + 1, R, p);
  tr->maintain();
}
int segQuery(SEG *tr, int L, int R, int qL, int qR) {
  if (!tr) return 0;
  if (L == qL && R == qR) return tr->v;
  int M = L + ((R - L) >> 1);
  if (qR <= M)
    return segQuery(tr->ch[0], L, M, qL, qR);
  else if (qL > M)
    return segQuery(tr->ch[1], M + 1, R, qL, qR);
  else
    return segQuery(tr->ch[0], L, M, qL, M) +
           segQuery(tr->ch[1], M + 1, R, M + 1, qR);
}
int query(int l, int r) { return r - l + 1 - segQuery(root, 1, N, l, r); }
bool solve() {
  int min_v = S.begin()->first;
  set<pair<int, int> >::iterator sit = S.lower_bound(make_pair(min_v, p));
  if (sit == S.end() || sit->first != min_v) return false;
  ans += query(p, sit->second);
  p = sit->second + 1;
  if (p > N) p -= N;
  segSetVal(root, 1, N, sit->second);
  S.erase(sit);
  return true;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    int v;
    scanf("%d", &v);
    S.insert(make_pair(v, i));
  }
  p = 1;
  for (int i = 0; i < N; ++i) {
    while (!solve()) {
      ans += query(p, N);
      p = 1;
    }
  }
  cout << ans << endl;
}
