#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { cin >> x; }
void _R(int64_t &x) { cin >> x; }
void _R(double &x) { cin >> x; }
void _R(long double &x) { cin >> x; }
void _R(char &x) { cin >> x; }
void _R(char *x) { cin >> x; }
void _R(string &x) { cin >> x; }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { cout << x; }
void _W(const int64_t &x) { cout << x; }
void _W(const double &x) { cout << fixed << setprecision(8) << x; }
void _W(const long double &x) { cout << fixed << setprecision(16) << x; }
void _W(const char &x) { cout << x; }
void _W(const char *x) { cout << x; }
void _W(const string &x) { cout << x; }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  cout << ' ';
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) cout << ' ';
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  cout << (sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int N = 100009;
vector<int> g[N];
int a[N];
int BIT[N];
int nxt[N];
int prv[N];
int n;
void add(int pos, int val) {
  assert(pos >= 1);
  while (pos <= n) {
    BIT[pos] += val;
    pos += pos & (-pos);
  }
}
int query(int pos) {
  assert(pos <= n && pos >= 0);
  int res = 0;
  while (pos > 0) {
    res += BIT[pos];
    pos -= pos & (-pos);
  }
  return res;
}
int query(int l, int r) { return query(r) - query(l - 1); }
void solve() {
  R(n);
  for (int i = (1); i < (n + 1); i++) {
    R(a[i]);
    g[a[i]].push_back(i);
    add(i, 1);
    nxt[i] = i + 1;
    prv[i] = i - 1;
  }
  nxt[n] = 1;
  prv[1] = n;
  long long ans = 0;
  int del = 0;
  vector<int> elems(n);
  for (int i = (1); i < (n + 1); i++) {
    elems[i - 1] = a[i];
  }
  (sort(elems.begin(), elems.end()),
   elems.resize(distance(elems.begin(), unique(elems.begin(), elems.end()))));
  int pos = 1;
  for (auto i : elems) {
    if (g[i][0] < pos) {
      int npos = -1;
      for (auto np : g[i]) {
        if (np < pos) {
          npos = np;
        } else {
          break;
        }
      }
      ans += 1LL * query(pos, n);
      ans += 1LL * query(1, npos);
      del += (int)(g[i]).size();
      if (del < n) {
        pos = npos;
        while (a[pos] <= i) {
          pos = nxt[pos];
        }
      }
      for (auto id : g[i]) {
        add(id, -1);
        int p_id = prv[id];
        int p_nx = nxt[id];
        nxt[p_id] = p_nx;
        prv[p_nx] = p_id;
      }
    } else {
      ans += 1LL * query(pos, g[i].back());
      del += (int)(g[i]).size();
      if (del < n) {
        pos = g[i].back();
        while (a[pos] <= i) {
          pos = nxt[pos];
        }
      }
      for (auto id : g[i]) {
        add(id, -1);
        int p_id = prv[id];
        int p_nx = nxt[id];
        nxt[p_id] = p_nx;
        prv[p_nx] = p_id;
      }
    }
  }
  W(ans);
}
int MULTITEST = 0;
void solve_pre() {}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve_pre();
  if (MULTITEST) {
    int t;
    R(t);
    for (int testcase = 1; testcase <= t; testcase++) {
      solve();
    }
  } else {
    solve();
  }
  return 0;
}
