#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
long long f[maxn];
vector<int> v[maxn];
int n;
struct card {
  long long num, pos;
};
card a[maxn];
bool cmp(card a, card b) {
  return a.num < b.num || (a.num == b.num && a.pos < b.pos);
}
int lowbit(int x) { return x & (-x); }
long long findsum(int p) {
  long long sum = 0;
  int i;
  for (i = p; i > 0; i -= lowbit(i)) sum += f[i];
  return sum;
}
void update(int p, long long val) {
  int i;
  for (i = p; i <= n; i += lowbit(i)) f[i] += val;
}
int main() {
  int i;
  scanf("%d", &n);
  memset(f, 0, sizeof(f));
  for (i = 1; i <= n; i++) {
    scanf("%I64d", &a[i].num);
    a[i].pos = i;
    update(i, 1);
    v[a[i].num].push_back(i);
  }
  sort(a + 1, a + n + 1, cmp);
  long long ans = 0;
  a[0].num = 0;
  for (i = 1; i <= n; i++)
    if (a[i].num != a[i - 1].num) sort(v[a[i].num].begin(), v[a[i].num].end());
  int last;
  last = ans = a[1].pos;
  update(a[1].pos, -1);
  for (i = 2; i <= n; i++) {
    int pos = upper_bound(v[a[i].num].begin(), v[a[i].num].end(), last) -
              v[a[i].num].begin();
    if (pos == v[a[i].num].size()) pos = 0;
    pos = v[a[i].num][pos];
    if (pos >= last)
      ans += findsum(pos) - findsum(last);
    else
      ans += findsum(pos) + findsum(n) - findsum(last);
    last = pos;
    update(pos, -1);
  }
  printf("%I64d\n", ans);
  return 0;
}
