#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 5;
struct BTree {
  int sum[MX], Max;
  BTree() {
    Max = MX;
    memset(sum, 0, sizeof(sum));
  }
  inline int lowbit(int x) { return x & (-x); }
  void add(int x, int val) {
    while (x <= Max) {
      sum[x] += val;
      x += lowbit(x);
    }
  }
  int tot(int x) {
    int ret = 0;
    while (x) {
      ret += sum[x];
      x -= lowbit(x);
    }
    return ret;
  }
  void clear() { memset(sum, 0, sizeof(sum)); }
  int query(int l, int r) {
    if (l > r) return 0;
    return tot(r) - tot(l - 1);
  }
} t;
struct node {
  int x, id;
} a[MX];
bool cmp(node p1, node p2) {
  if (p1.x != p2.x) return p1.x < p2.x;
  return p1.id < p2.id;
}
vector<int> v[MX];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].x), a[i].id = i;
  sort(a + 1, a + n + 1, cmp);
  int sz = 0;
  v[++sz].push_back(a[1].id);
  for (int i = 2; i <= n; i++) {
    if (a[i].x != a[i - 1].x) ++sz;
    v[sz].push_back(a[i].id);
  }
  for (int i = 1; i <= n; i++) t.add(i, 1);
  int last = 0;
  long long ans = 0;
  for (int i = 1; i <= sz; i++) {
    int pos = upper_bound(v[i].begin(), v[i].end(), last) - v[i].begin();
    int cnt = v[i].size();
    int bk = v[i][cnt - 1];
    if (pos > 0)
      ans += t.query(last, n);
    else
      ans += t.query(last + 1, bk);
    for (int j = cnt - 1; j >= pos; j--) {
      t.add(v[i][j], -1);
      v[i].pop_back();
    }
    if (pos > 0) {
      i--;
      last = 0;
    } else {
      last = bk == n ? 0 : bk;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
