#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, T = 1 << 18;
long long n, a[N];
vector<long long> b[N];
set<long long> s;
long long t[2 * T];
void build() {
  for (long long i = 0; i < 2 * T; ++i) {
    t[i] = 0;
  }
}
void upd(long long v, long long x) {
  v += T;
  t[v] = x;
  v /= 2;
  while (v > 0) {
    t[v] = t[2 * v] + t[2 * v + 1];
    v /= 2;
  }
}
long long get_sum(long long v, long long tl, long long tr, long long L,
                  long long R) {
  if (tl > R || tr < L) return 0;
  if (L <= tl && tr <= R) return t[v];
  long long tm = (tl + tr) / 2;
  return get_sum(2 * v, tl, tm, L, R) + get_sum(2 * v + 1, tm + 1, tr, L, R);
}
long long solve1() {
  queue<long long> q, mins;
  for (long long i = 0; i < n; ++i) q.push(a[i]);
  sort(a, a + n);
  for (long long i = 0; i < n; ++i) mins.push(a[i]);
  long long ans = 0;
  while (!q.empty()) {
    long long cur = q.front();
    q.pop();
    ++ans;
    if (cur == mins.front()) {
      mins.pop();
      continue;
    }
    q.push(cur);
  }
  return ans;
}
long long bin_search(vector<long long> &v, long long x) {
  long long L = -1, R = (long long)v.size();
  while (L + 1 != R) {
    long long M = (L + R) / 2;
    if (v[M] <= x) {
      L = M;
    } else {
      R = M;
    }
  }
  return L;
}
signed main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (long long i = 0; i < n; ++i) cin >> a[i];
  build();
  for (long long i = 0; i < n; ++i) {
    b[a[i]].push_back(i);
    s.insert(a[i]);
    upd(i, 1);
  }
  queue<long long> q;
  for (auto el : s) q.push(el);
  long long p = 0;
  long long ans = 0;
  while (!q.empty()) {
    long long cur = q.front();
    q.pop();
    if (p >= n) p -= n;
    long long pos = b[cur].back();
    if (b[cur][0] >= p) {
      ans += get_sum(1, 0, T - 1, p, b[cur].back());
      p = b[cur].back() + 1;
    } else {
      long long pos = b[cur][bin_search(b[cur], p)];
      if (pos == p) {
        pos = b[cur][bin_search(b[cur], p - 1)];
      }
      ans += get_sum(1, 0, T - 1, p, n - 1);
      ans += get_sum(1, 0, T - 1, 0, pos);
      p = pos + 1;
    }
    for (auto i : b[cur]) {
      upd(i, 0);
    }
  }
  cout << ans;
}
