#include <bits/stdc++.h>
using namespace std;
int ran() { return (rand() << 15) + rand(); }
int IN() {
  int first = 0;
  char c;
  for (; (c = getchar()) < 48 && c ^ '-' || c > 57;)
    ;
  bool f = c == '-';
  if (f) (c = getchar());
  for (; c > 47 && c < 58; (c = getchar()))
    first = (first << 1) + (first << 3) + c - 48;
  return f ? -first : first;
}
void print(int first, char en = '\n') {
  printf("%d", first);
  if (en) putchar(en);
}
void hello() { freopen("remi.in", "r", stdin); }
const int inf = 1e9;
int n, L, R, a[100010], g[100010];
struct node {
  int mi, sm;
} t[400010];
void upd(int u) {
  t[u].mi =
      a[t[u << 1].mi] <= a[t[u << 1 | 1].mi] ? t[u << 1].mi : t[u << 1 | 1].mi;
  t[u].sm = t[u << 1].sm + t[u << 1 | 1].sm;
}
void build(int u, int ll, int rr) {
  if (ll == rr) {
    t[u].mi = ll;
    a[ll] = g[ll] = IN();
    t[u].sm = 1;
    return;
  }
  int mm = ll + rr >> 1;
  build(u << 1, ll, mm);
  build(u << 1 | 1, mm + 1, rr);
  upd(u);
}
node QS(int u, int ll, int rr) {
  if (L <= ll && rr <= R) {
    return t[u];
  }
  int mm = ll + rr >> 1;
  node res{0, 0};
  if (L <= mm) res = QS(u << 1, ll, mm);
  if (R > mm) {
    auto tmp = QS(u << 1 | 1, mm + 1, rr);
    if (a[tmp.mi] < a[res.mi]) res.mi = tmp.mi;
    res.sm += tmp.sm;
  }
  return res;
}
node qs(int l, int r) {
  L = l;
  R = r;
  assert(1 <= L && L <= R && R <= n);
  return QS(1, 1, n);
}
void lu(int u, int ll, int rr) {
  if (ll == rr) {
    assert(t[u].sm == 1 && t[u].mi != 0);
    t[u].mi = 0;
    t[u].sm = 0;
    return;
  }
  int mm = ll + rr >> 1;
  if (L <= mm)
    lu(u << 1, ll, mm);
  else
    lu(u << 1 | 1, mm + 1, rr);
  upd(u);
}
void gao(int po) {
  L = po;
  assert(1 <= L && L <= n);
  lu(1, 1, n);
}
int main(int argc, char* argv[]) {
  n = IN();
  a[0] = inf;
  build(1, 1, n);
  sort(g + 1, g + n + 1);
  long long ans = 0;
  int po = 0;
  for (int _r = n, i = 1; i <= _r; ++i) {
    int sd = g[i];
    auto it = qs(po + 1, n);
    if (a[it.mi] == sd) {
      ans += qs(po + 1, it.mi).sm;
      po = it.mi;
      gao(po);
    } else {
      ans += it.sm;
      it = qs(1, po - 1);
      assert(a[it.mi] == sd);
      ans += qs(1, it.mi).sm;
      po = it.mi;
      gao(po);
    }
    po %= n;
  }
  cout << ans << endl;
}
