#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int e = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    e = e * 10 + (ch - '0');
    ch = getchar();
  }
  return e * f;
}
const int maxn = 1e5 + 7;
const int inf = 0x3f3f3f3f;
int a[maxn];
struct SegmentTree {
  int tree[maxn << 2], rcd[maxn];
  void Push_up(int key) {
    if (rcd[tree[key << 1]] <= rcd[tree[key << 1 | 1]])
      tree[key] = tree[key << 1];
    else
      tree[key] = tree[key << 1 | 1];
  }
  void Buildtree(int l, int r, int key) {
    if (l == r) {
      tree[key] = l;
      return;
    }
    int mid = (l + r) >> 1;
    Buildtree(l, mid, key << 1);
    Buildtree(mid + 1, r, key << 1 | 1);
    Push_up(key);
  }
  void Update(int key, int pos, int l, int r) {
    if (l == r) {
      rcd[l] = inf;
      return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid)
      Update(key << 1, pos, l, mid);
    else
      Update(key << 1 | 1, pos, mid + 1, r);
    Push_up(key);
  }
  int Query(int l, int r, int ls, int rs, int key) {
    if (ls <= l && r <= rs) return tree[key];
    int mid = (l + r) >> 1;
    if (rs <= mid)
      return Query(l, mid, ls, rs, key << 1);
    else if (mid < ls)
      return Query(mid + 1, r, ls, rs, key << 1 | 1);
    else {
      int t1 = Query(l, mid, ls, rs, key << 1),
          t2 = Query(mid + 1, r, ls, rs, key << 1 | 1);
      return (rcd[t1] <= rcd[t2] ? t1 : t2);
    }
  }
} keke;
struct BIT {
  int k[maxn], len;
  void init(int tt) {
    len = tt;
    memset(k, 0, sizeof(k));
  }
  inline int lowbit(int x) { return x & -x; }
  void Add(int pos, int val) {
    while (pos <= len) {
      k[pos] += val;
      pos += lowbit(pos);
    }
  }
  int Sum(int pos) {
    int ans = 0;
    while (pos) {
      ans += k[pos];
      pos -= lowbit(pos);
    }
    return ans;
  }
} haha;
int main() {
  int n = Read();
  haha.init(n);
  for (int i = 1; i <= n; i++) keke.rcd[i] = a[i] = Read(), haha.Add(i, 1);
  keke.Buildtree(1, n, 1);
  sort(a + 1, a + n + 1);
  long long ans = 0;
  for (int i = 1, last = 1, tmp; i <= n; i++) {
    tmp = keke.Query(1, n, last, n, 1);
    if (keke.rcd[tmp] == a[i]) {
      ans += (haha.Sum(tmp) - haha.Sum(last - 1));
      haha.Add(tmp, -1);
      keke.Update(1, tmp, 1, n);
    } else {
      tmp = keke.tree[1];
      ans += (haha.Sum(tmp) + (haha.Sum(n) - haha.Sum(last - 1)));
      haha.Add(tmp, -1);
      keke.Update(1, tmp, 1, n);
    }
    last = tmp + 1;
    if (last > n) last = 1;
  }
  cout << ans << endl;
  return 0;
}
