#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 7;
const int INF = (1e6);
int n, k, a[maxn], r[maxn], cur;
long long ans;
struct Node {
  Node *l, *r;
  int v, mini, size, id;
  Node(int val, Node *null) {
    v = mini = val, l = r = null;
    size = 1;
  }
  void maintain() {
    size = l->size + r->size + 1;
    mini = min(min(l->mini, r->mini), v);
  }
} * root, *null;
typedef pair<Node *, Node *> pat;
pat t1, t2;
Node *Build(int l, int r) {
  if (l > r) return null;
  int mid = (l + r) >> 1;
  Node *o = new Node(a[mid], null);
  o->l = Build(l, mid - 1), o->r = Build(mid + 1, r);
  o->maintain();
  return o;
}
bool judge(int x, int y) {
  cur = (cur == n) ? (1) : (cur + 1);
  return ((r[cur] % (x + y)) < x);
}
Node *merge(Node *p, Node *q) {
  if (p == null) return q;
  if (q == null) return p;
  if (judge(p->size, q->size)) {
    p->r = merge(p->r, q);
    p->maintain();
    return p;
  } else {
    q->l = merge(p, q->l);
    q->maintain();
    return q;
  }
}
pat split(Node *o, int k) {
  if ((o == null) || (o->size <= k)) return make_pair(o, null);
  if (k == 0) return make_pair(null, o);
  if (o->l->size >= k) {
    pat temp = split(o->l, k);
    Node *p = new Node(o->v, null);
    p->l = temp.second;
    p->r = o->r;
    p->maintain();
    return make_pair(temp.first, p);
  } else {
    pat temp = split(o->r, k - o->l->size - 1);
    Node *p = new Node(o->v, null);
    p->l = o->l;
    p->r = temp.first;
    p->maintain();
    return make_pair(p, temp.second);
  }
}
int query(Node *o) {
  if (o->mini == o->l->mini) return query(o->l);
  if (o->v == o->mini) return o->l->size + 1;
  return o->l->size + 1 + query(o->r);
}
int main() {
  srand(192671708);
  scanf("%d", &n);
  cur = n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), r[i] = (rand() << 15) ^ rand();
  null = new Node(0, NULL);
  null->l = null->r = null;
  null->size = 0, null->mini = null->v = INF;
  root = Build(1, n);
  for (int i = 1; i <= n; i++) {
    k = query(root);
    ans += (long long)(k);
    t1 = split(root, k - 1);
    t2 = split(t1.second, 1);
    root = merge(t2.second, t1.first);
  }
  printf("%I64d\n", ans);
  return 0;
}
