#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int a[MAXN], b[MAXN];
vector<int> loc[MAXN];
int tree[MAXN];
int lowbit(int x) { return x & (-x); }
void add(int loc, int x) {
  for (int i = loc; i < MAXN; i += lowbit(i)) tree[i] += x;
}
int sum(int loc) {
  int res = 0;
  for (int i = loc; i; i -= lowbit(i)) res += tree[i];
  return res;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
int nxt[MAXN];
int cnt[MAXN];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    loc[a[i]].push_back(i);
    cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) add(i, 1);
  int flg = 0;
  for (int i = 1; i <= 100000; i++) {
    if (!cnt[i]) continue;
    int l = 0, r = cnt[i] - 1;
    if (flg > loc[i][r] || flg < loc[i][l]) {
      nxt[flg] = loc[i][l];
      for (int j = 0; j < r; j++) nxt[loc[i][j]] = loc[i][j + 1];
      flg = loc[i][r];
    } else {
      while (l < r - 1) {
        int mid = (l + r) >> 1;
        if (loc[i][mid] < flg)
          l = mid;
        else
          r = mid;
      }
      nxt[flg] = loc[i][r];
      int sz = loc[i].size();
      for (int j = r; j < sz - 1; j++) nxt[loc[i][j]] = loc[i][j + 1];
      nxt[loc[i][sz - 1]] = loc[i][0];
      for (int j = 0; j < l; j++) nxt[loc[i][j]] = loc[i][j + 1];
      flg = loc[i][l];
    }
  }
  flg = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (nxt[flg] < flg) {
      ans = ans + sum(flg + 1, n) + sum(1, nxt[flg]);
    } else {
      ans = ans + sum(flg + 1, nxt[flg]);
    }
    flg = nxt[flg];
    add(flg, -1);
  }
  return 0 * printf("%I64d\n", ans);
}
