#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int _t;
struct Splay_tree {
  int root, q[N];
  bool rev[N];
  int key[N], sz[N], f[N], ch[N][2], add[N];
  void init() { root = _t = 0; }
  void rev1(int x) {
    if (x) swap(ch[x][0], ch[x][1]), rev[x] ^= 1;
  }
  void add1(int x, int p) {
    if (x) key[x] += p, add[x] += p;
  }
  inline void nw(int &x, int val, int fa) {
    x = ++_t, key[x] = val, f[x] = fa, sz[x] = 1;
    ch[x][0] = ch[x][1] = 0;
    add[x] = rev[x] = 0;
  }
  inline void pd(int x) {
    if (add[x]) add1(ch[x][0], add[x]), add1(ch[x][1], add[x]), add[x] = 0;
    if (rev[x]) rev1(ch[x][0]), rev1(ch[x][1]), rev[x] = 0;
  }
  inline void up(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; }
  void rotate(int x) {
    int y = f[x], w = ch[y][1] == x;
    ch[y][w] = ch[x][w ^ 1];
    if (ch[x][w ^ 1]) f[ch[x][w ^ 1]] = y;
    if (f[y]) {
      int z = f[y];
      if (ch[z][0] == y) ch[z][0] = x;
      if (ch[z][1] == y) ch[z][1] = x;
    }
    f[x] = f[y], ch[x][w ^ 1] = y, f[y] = x, up(y);
  }
  void splay(int x, int w) {
    int s = 1, i = x, y;
    q[1] = x;
    while (f[i]) q[++s] = i = f[i];
    while (s) pd(q[s--]);
    while (f[x] != w) {
      y = f[x];
      if (f[y] != w) {
        if ((ch[f[y]][0] == y) ^ (ch[y][0] == x))
          rotate(x);
        else
          rotate(y);
      }
      rotate(x);
    }
    if (!w) root = x;
    up(x);
  }
  void build(int &x, int l, int r, int fa = 0) {
    if (l > r) return;
    int mid = l + r >> 1;
    nw(x, mid, fa);
    build(ch[x][0], l, mid - 1, x);
    build(ch[x][1], mid + 1, r, x);
    up(x);
  }
  inline int find(int _key) {
    if (!root) return 0;
    int x = root;
    for (pd(x); x && key[x] != _key;) x = ch[x][key[x] < _key];
    if (x) splay(x, 0);
    return x;
  }
  inline void Delete(int r) {
    splay(r, 0);
    int pos = sz[ch[r][0]];
    splay(kth(pos), 0);
    splay(kth(pos + 2), root);
    ch[ch[root][1]][0] = 0;
    up(ch[root][1]), up(root);
  }
  inline int kth(int k) {
    if (k > sz[root] || k <= 0) return 0;
    int x = root, tmp;
    while (1) {
      pd(x), tmp = sz[ch[x][0]] + 1;
      if (k == tmp) break;
      if (k < tmp)
        x = ch[x][0];
      else
        k -= tmp, x = ch[x][1];
    }
    return x;
  }
  void reverse(int a, int b) {
    splay(kth(a), 0), splay(kth(b + 2), root);
    rev1(ch[ch[root][1]][0]);
  }
  inline void ins(int _key) {
    if (!root) {
      nw(root, _key, 0);
      return;
    }
    int x = root, y = 0;
    while (x) pd(x), y = x, sz[x]++, x = ch[x][key[x] < _key];
    nw(ch[y][key[y] < _key], _key, y);
    splay(ch[y][key[y] < _key], 0);
  }
  void cut(int a, int b, int c) {
    splay(kth(a), 0), splay(kth(b + 2), root);
    int tmp = ch[ch[root][1]][0];
    ch[ch[root][1]][0] = 0;
    pd(ch[root][1]), pd(root);
    splay(kth(c + 1), 0);
    splay(kth(c + 2), root);
    ch[ch[root][1]][0] = tmp;
    f[ch[ch[root][1]][0]] = ch[root][1];
    up(ch[root][1]), up(root);
  }
  void inorder(int r) {
    if (!r) return;
    inorder(ch[r][0]);
    printf("%d ", key[r]);
    inorder(ch[r][1]);
  }
} spt;
int n, now, f[N], ed;
struct node {
  int x, idx;
  bool operator<(const node &b) const {
    return x != b.x ? x < b.x : idx < b.idx;
  }
} a[N];
set<node> st;
set<node>::iterator it;
int find(int x) { return f[x] != x ? f[x] = find(f[x]) : x; }
int getkth(int x) {
  int rt = spt.find(x);
  return spt.sz[spt.ch[rt][0]] + 1;
}
int del() {
  it = st.lower_bound({a[ed].x, now});
  if (it == st.end() || it->x > a[ed].x) {
    it = st.lower_bound({a[ed].x, 1});
  }
  int tmp = it->idx;
  st.erase(it);
  return tmp;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i].x), a[i].idx = i, f[i] = i, st.insert(a[i]);
  sort(a + 1, a + 1 + n);
  spt.build(spt.root, 1, n + 2);
  now = 1, ed = 1;
  long long ans = 0;
  while (ed <= n) {
    int cur = getkth(now + 1), tp = del();
    int tmp = getkth(tp + 1);
    if (tmp >= cur)
      ans += tmp - cur + 1;
    else {
      ans += spt.sz[spt.root] - cur + tmp - 1;
    }
    int rt = spt.find(tp + 1);
    spt.Delete(rt);
    if (tp < n)
      f[tp] = tp + 1;
    else
      f[tp] = 1;
    if (ed < n) now = find(f[tp]);
    ed++;
  }
  printf("%lld\n", ans);
  return 0;
}
