#include <bits/stdc++.h>
#pragma G++ optimize "O2"
#pragma GCC optimize "O2"
#pragma G++ optimize "O3"
#pragma GCC optimize "O3"
#pragma G++ optimize "Ofast"
#pragma GCC optimize "Ofast"
using namespace std;
const long long maxn = 100010;
struct node {
  long long num;
  long long cunzai;
  long long pos;
} T[maxn << 4];
node merge(node a, node b) {
  node ret;
  memset(&ret, 0, sizeof(node));
  if (a.num < b.num) {
    ret.num = a.num;
    ret.pos = a.pos;
  } else if (a.num > b.num) {
    ret.num = b.num;
    ret.pos = b.pos;
  } else {
    ret.num = a.num;
    ret.pos = min(a.pos, b.pos);
  }
  ret.cunzai = a.cunzai + b.cunzai;
  return ret;
}
long long a[maxn];
void build(long long id, long long l, long long r) {
  if (l == r) {
    T[id].num = a[l];
    T[id].pos = l;
    T[id].cunzai = 1;
    return;
  }
  long long mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  T[id] = merge(T[id << 1], T[id << 1 | 1]);
}
void update(long long id, long long l, long long r, long long x, node &v) {
  if (l == r) {
    T[id] = v;
    return;
  }
  long long mid = l + r >> 1;
  if (x <= mid) {
    update(id << 1, l, mid, x, v);
  } else {
    update(id << 1 | 1, mid + 1, r, x, v);
  }
  T[id] = merge(T[id << 1], T[id << 1 | 1]);
}
node query(long long id, long long l, long long r, long long x, long long y) {
  if (x <= l && r <= y) {
    return T[id];
  }
  node ret;
  memset(&ret, 0, sizeof(node));
  bool ok1 = false;
  long long mid = l + r >> 1;
  if (x <= mid) {
    ret = query(id << 1, l, mid, x, y);
    ok1 = true;
  }
  if (y > mid) {
    if (ok1) {
      ret = merge(ret, query(id << 1 | 1, mid + 1, r, x, y));
    } else {
      ret = query(id << 1 | 1, mid + 1, r, x, y);
    }
  }
  return ret;
}
long long n;
long long pos[maxn];
long long aa[maxn];
long long nxt[maxn];
long long ans = 0;
long long getcnt = 0;
signed main() {
  scanf("%lld", &n);
  for (register long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    aa[i] = a[i];
    if (!pos[a[i]]) pos[a[i]] = i;
  }
  sort(aa + 1, aa + 1 + n);
  build(1, 1, n);
  long long now = aa[2];
  long long lastpos = pos[aa[1]];
  ans += lastpos;
  node tnxt;
  tnxt.cunzai = 0;
  tnxt.num = 0x3f3f3f3f;
  tnxt.pos = lastpos;
  update(1, 1, n, lastpos, tnxt);
  while (true) {
    if (getcnt == n - 1) break;
    node nodenow = query(1, 1, n, lastpos + 1, n);
    if (nodenow.num == now) {
      node tstep = query(1, 1, n, lastpos + 1, nodenow.pos);
      ans += tstep.cunzai;
      getcnt++;
      node nxt;
      nxt.num = 0x3f3f3f3f;
      nxt.cunzai = 0;
      nxt.pos = nodenow.pos;
      update(1, 1, n, nodenow.pos, nxt);
      lastpos = tstep.pos;
      now = aa[getcnt + 2];
    } else {
      node minnode = query(1, 1, n, 1, lastpos - 1);
      node bak = query(1, 1, n, lastpos + 1, n);
      node pre = query(1, 1, n, 1, minnode.pos);
      ans += bak.cunzai + pre.cunzai;
      getcnt++;
      node nxt;
      nxt.num = 0x3f3f3f3f;
      nxt.cunzai = 0;
      nxt.pos = minnode.pos;
      update(1, 1, n, minnode.pos, nxt);
      lastpos = minnode.pos;
      now = aa[getcnt + 2];
      ;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
