#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
int t[4 * 100007], tree[100007], aa[100007], n;
set<int> s[100007];
void build(int nd, int tl, int tr) {
  if (tl == tr) {
    t[nd] = aa[tl];
    return;
  }
  int tm = (tl + tr) >> 1;
  int lc = nd << 1;
  int rc = lc | 1;
  build(lc, tl, tm);
  build(rc, tm + 1, tr);
  t[nd] = min(t[lc], t[rc]);
}
void update(int nd, int tl, int tr, int pos) {
  if (tl == tr) {
    t[nd] = 100000000;
    return;
  }
  int tm = (tl + tr) >> 1;
  int lc = nd << 1;
  int rc = lc | 1;
  if (pos <= tm)
    update(lc, tl, tm, pos);
  else
    update(rc, tm + 1, tr, pos);
  t[nd] = min(t[lc], t[rc]);
}
int query(int nd, int tl, int tr, int l, int r) {
  if (tl >= l && tr <= r) return t[nd];
  int tm = (tl + tr) >> 1;
  int lc = nd << 1;
  int rc = lc | 1;
  if (r <= tm)
    return query(lc, tl, tm, l, r);
  else if (l > tm)
    return query(rc, tm + 1, tr, l, r);
  else
    return min(query(lc, tl, tm, l, r), query(rc, tm + 1, tr, l, r));
}
void update(int idx, int val) {
  while (idx <= n) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}
int query(int idx) {
  int sum = 0;
  while (idx) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}
int query(int idi, int idj) {
  int sum = 0;
  while (idi <= idj) {
    sum += tree[idj];
    idj -= (idj & -idj);
  }
  idi--;
  while (idi != idj) {
    sum -= tree[idi];
    idi -= (idi & -idi);
  }
  return sum;
}
int main() {
  long long ans = 0;
  int i, j, k, in1, in2, c;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &aa[i]);
    s[aa[i]].insert(i);
    update(i, 1);
  }
  build(1, 1, n);
  c = 0;
  for (i = 1; i <= n; i++) {
    int b = 100000000;
    in1 = 100000000;
    if (c < n) {
      in1 = query(1, 1, n, c + 1, n);
      if (in1 != 100000000) {
        auto it = s[in1].lower_bound(c + 1);
        b = *it;
      }
    }
    int a = 100000000;
    in2 = 100000000;
    if (c > 1) {
      in2 = query(1, 1, n, 1, c - 1);
      if (in2 != 100000000) {
        auto it = s[in2].lower_bound(1);
        a = *it;
      }
    }
    if (in2 < in1) {
      ans += query(c + 1, n);
      ans += query(a);
      update(1, 1, n, a);
      update(a, -1);
      c = a;
    } else {
      ans += query(c + 1, b);
      update(1, 1, n, b);
      update(b, -1);
      c = b;
    };
  }
  cout << ans << endl;
  return 0;
}
