#include <bits/stdc++.h>
using namespace std;
int val[100009];
int n;
int ar[100009];
void upd(int idx, int v) {
  while (idx <= n) {
    ar[idx] += v;
    idx += (idx & (-idx));
  }
}
int quer(int idx) {
  int r = 0;
  while (idx) {
    r += ar[idx];
    idx -= (idx & (-idx));
  }
  return r;
}
struct node {
  int ind, mn;
  node(int a, int b) {
    ind = a;
    mn = b;
  }
  node() {}
} tree[4 * 100009];
node mergenode(node a, node b) {
  if (a.mn < b.mn)
    return a;
  else if (a.mn > b.mn)
    return b;
  else if (a.ind < b.ind)
    return a;
  else
    return b;
}
void build(int ind, int st, int ed) {
  if (st == ed) {
    tree[ind] = node(st, val[st]);
  } else {
    build(2 * ind, st, (st + ed) / 2);
    build(2 * ind + 1, (st + ed) / 2 + 1, ed);
    tree[ind] = mergenode(tree[2 * ind], tree[2 * ind + 1]);
  }
}
void update(int ind, int st, int ed, int i) {
  if (i < st || i > ed) {
    return;
  } else if (st == ed) {
    tree[ind] = node(st, 10000000);
    return;
  }
  update(2 * ind, st, (st + ed) / 2, i);
  update(2 * ind + 1, (st + ed) / 2 + 1, ed, i);
  tree[ind] = mergenode(tree[2 * ind], tree[2 * ind + 1]);
}
node query(int ind, int st, int ed, int i, int j) {
  if (st > j || ed < i) {
    return node(st, 100000000);
  } else if (st >= i && ed <= j) {
    return tree[ind];
  } else
    return mergenode(query(2 * ind, st, (st + ed) / 2, i, j),
                     query(2 * ind + 1, (st + ed) / 2 + 1, ed, i, j));
}
int main() {
  int i, j;
  long long int tot = 0ll;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
    upd(i, 1);
  }
  build(1, 1, n);
  int lst = 1;
  node nw1, nw2;
  for (i = 0; i < n; i++) {
    nw1 = query(1, 1, n, lst, n);
    nw2 = query(1, 1, n, 1, lst);
    if (nw1.mn <= nw2.mn) {
      tot += (quer(nw1.ind) - quer(lst - 1));
      update(1, 1, n, nw1.ind);
      upd(nw1.ind, -1);
      lst = nw1.ind;
    } else {
      tot += (quer(n) - quer(lst - 1) + quer(nw2.ind));
      update(1, 1, n, nw2.ind);
      upd(nw2.ind, -1);
      lst = nw2.ind;
    }
  }
  printf("%lld\n", tot);
  return 0;
}
