#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int n;
long long a[100039], b[100039];
struct SegTree {
  struct SegNode {
    int l, r;
    long long add;
    long long sum, max, min;
  } tree[100039 << 2];
  void build(int p, int l, int r, int fg) {
    tree[p].add = 0;
    tree[p].l = l, tree[p].r = r;
    if (l == r) {
      if (fg)
        tree[p].sum = tree[p].max = tree[p].min = a[l];
      else
        tree[p].sum = tree[p].max = tree[p].min = 0;
      return;
    }
    int mid = (l + r) / 2;
    build(p * 2, l, mid, fg);
    build(p * 2 + 1, mid + 1, r, fg);
    tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
    tree[p].max = max(tree[p * 2].max, tree[p * 2 + 1].max);
    tree[p].min = min(tree[p * 2].min, tree[p * 2 + 1].min);
  }
  void spread(int p) {
    if (tree[p].add) {
      tree[p * 2].sum += tree[p].add * (tree[p * 2].r - tree[p * 2].l + 1);
      tree[p * 2 + 1].sum +=
          tree[p].add * (tree[p * 2 + 1].r - tree[p * 2 + 1].l + 1);
      tree[p * 2].max += tree[p].add;
      tree[p * 2 + 1].max += tree[p].add;
      tree[p * 2].min += tree[p].add;
      tree[p * 2 + 1].min += tree[p].add;
      tree[p * 2].add += tree[p].add;
      tree[p * 2 + 1].add += tree[p].add;
      tree[p].add = 0;
    }
  }
  void change(int p, int l, int r, long long d) {
    if (l <= tree[p].l && r >= tree[p].r) {
      tree[p].sum += d * (tree[p].r - tree[p].l + 1);
      tree[p].max += d;
      tree[p].min += d;
      tree[p].add += d;
      return;
    }
    spread(p);
    int mid = (tree[p].l + tree[p].r) / 2;
    if (l <= mid) change(p * 2, l, r, d);
    if (r > mid) change(p * 2 + 1, l, r, d);
    tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
    tree[p].max = max(tree[p * 2].max, tree[p * 2 + 1].max);
    tree[p].min = min(tree[p * 2].min, tree[p * 2 + 1].min);
  }
  long long ask4sum(int p, int l, int r) {
    if (l <= tree[p].l && r >= tree[p].r) return tree[p].sum;
    spread(p);
    int mid = (tree[p].l + tree[p].r) / 2;
    long long val = 0;
    if (l <= mid) val += ask4sum(p * 2, l, r);
    if (r > mid) val += ask4sum(p * 2 + 1, l, r);
    return val;
  }
  long long ask4max(int p, int l, int r) {
    if (l <= tree[p].l && r >= tree[p].r) return tree[p].max;
    spread(p);
    int mid = (tree[p].l + tree[p].r) / 2;
    long long val = -0x3f3f3f3f;
    if (l <= mid) val = max(val, ask4max(p * 2, l, r));
    if (r > mid) val = max(val, ask4max(p * 2 + 1, l, r));
    return val;
  }
  long long ask4min(int p, int l, int r) {
    if (l <= tree[p].l && r >= tree[p].r) return tree[p].min;
    spread(p);
    int mid = (tree[p].l + tree[p].r) / 2;
    long long val = 0x3f3f3f3f;
    if (l <= mid) val = min(val, ask4min(p * 2, l, r));
    if (r > mid) val = min(val, ask4min(p * 2 + 1, l, r));
    return val;
  }
  int ask4firstless(int p, int l, int r, long long x) {
    if (tree[p].l == tree[p].r) {
      if (tree[p].min <= x)
        return tree[p].l;
      else
        return -1;
    }
    spread(p);
    if (l <= tree[p].l && r >= tree[p].r && tree[p].min > x) return -1;
    int mid = (tree[p].l + tree[p].r) / 2;
    int jd = -1;
    if (l <= mid) jd = ask4firstless(p * 2, l, r, x);
    if (jd != -1) return jd;
    if (r > mid) jd = ask4firstless(p * 2 + 1, l, r, x);
    return jd;
  }
} T, CT;
int main() {
  scanf("%d", &n);
  for (int i = (1); i < n + 1; i++) {
    scanf("%lld", &a[i]);
    b[i] = a[i];
  }
  T.build(1, 1, n, 1);
  CT.build(1, 1, n, 0);
  sort(b + 1, b + 1 + n);
  int cur = 1;
  long long ans = 0;
  for (int i = (1); i < n + 1; i++) {
    int x = b[i];
    int y = T.ask4firstless(1, cur, n, x);
    if (y == -1) {
      ans += n - cur + 1 - CT.ask4sum(1, cur, n);
      cur = 1;
      y = T.ask4firstless(1, cur, n, x);
      ans += y - CT.ask4sum(1, cur, y);
      CT.change(1, y, y, 1);
      cur = y + 1;
      if (cur == n + 1) cur = 1;
    } else {
      ans += y - cur + 1 - CT.ask4sum(1, cur, y);
      CT.change(1, y, y, 1);
      cur = y + 1;
      if (cur == n + 1) cur = 1;
    }
    T.change(1, y, y, 0x3f3f3f3f);
  }
  printf("%lld", ans);
  return 0;
}
