#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
struct lt {
  int l, r;
  int mi, pos;
  int count;
};
lt rt[100010 << 2];
int a[100010];
void updata(int i) {
  rt[i].count = rt[i << 1].count + rt[i << 1 | 1].count;
  if (rt[i << 1].mi <= rt[i << 1 | 1].mi) {
    rt[i].mi = rt[i << 1].mi;
    rt[i].pos = rt[i << 1].pos;
  } else {
    rt[i].mi = rt[i << 1 | 1].mi;
    rt[i].pos = rt[i << 1 | 1].pos;
  }
}
void build(int i, int l, int r) {
  rt[i].l = l;
  rt[i].r = r;
  if (l == r) {
    rt[i].count = 1;
    rt[i].mi = a[l - 1];
    rt[i].pos = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build(i << 1 | 1, mid + 1, r);
  updata(i);
}
void dele(int i, int d) {
  if (rt[i].l > d || rt[i].r < d) return;
  if (rt[i].l == rt[i].r) {
    rt[i].mi = inf;
    rt[i].pos = -1;
    rt[i].count = 0;
    return;
  }
  dele(i << 1, d);
  dele(i << 1 | 1, d);
  updata(i);
}
int query_c(int i, int l, int r) {
  if (rt[i].l > r || rt[i].r < l) return 0;
  if (rt[i].l >= l && rt[i].r <= r) return rt[i].count;
  return query_c(i << 1, l, r) + query_c(i << 1 | 1, l, r);
}
pair<int, int> query_mi(int i, int l, int r) {
  if (rt[i].l > r || rt[i].r < l) return pair<int, int>(inf, inf);
  if (rt[i].l >= l && rt[i].r <= r) return pair<int, int>(rt[i].mi, rt[i].pos);
  return min(query_mi(i << 1, l, r), query_mi(i << 1 | 1, l, r));
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) scanf("%d", a + i);
    build(1, 1, n);
    int now = 0;
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int pos = rt[1].pos;
      if (pos > now)
        ans += query_c(1, now, pos);
      else {
        pair<int, int> temp = query_mi(1, now, n);
        if (temp.first == rt[1].mi) {
          pos = temp.second;
          ans += query_c(1, now, pos);
        } else
          ans += query_c(1, 1, pos) + query_c(1, now, n);
      }
      now = pos;
      dele(1, pos);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
