#include <bits/stdc++.h>
using namespace std;
inline void read(int &a) {
  int f = a = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') a = a * 10 + ch - '0', ch = getchar();
  if (f) a = -a;
}
int K, f[1000 + 3], la = 1;
int n, a[100000 + 3];
long long s[1000 + 3];
inline int X(int x) { return (x - 1) / K + 1; }
int modify(int x) {
  a[x] = 1000000000;
  f[X(x)] = a[x];
  int xx = X(x), w = 1;
  int l = (xx - 1) * K + 1, r = xx * K;
  for (int i = l; i <= r && i <= n; i++) f[xx] = min(f[xx], a[i]);
  l = x + 1, r = n;
  while (l <= r && (l - 1) % K != 0) {
    if (a[l] != 1000000000) return l;
    l++;
  }
  while (l + K - 1 <= r) {
    if (s[X(l)] != 0) break;
    l += K;
  }
  while (l <= r && ((l - 1) % K != 0 || w)) {
    w = 0;
    if (a[l] != 1000000000) return l;
    l++;
  }
  l = 1;
  r = x - 1;
  while (l + K - 1 <= r) {
    if (s[X(l)] != 0) break;
    l += K;
  }
  w = 1;
  while (l <= r && ((l - 1) % K != 0 || w)) {
    w = 0;
    if (a[l] != 1000000000) return l;
    l++;
  }
}
int find(int x, long long &S) {
  S = 0;
  int mn = 1000000000;
  for (int i = 1; i <= X(n); i++) mn = min(f[i], mn);
  int l = x, r = n;
  while (l <= r && (l - 1) % K != 0) {
    if (a[l] != 1000000000) S++;
    if (mn == a[l]) {
      s[X(l)]--;
      return l;
    }
    l++;
  }
  while (l + K - 1 <= r) {
    if (mn == f[X(l)]) break;
    S += s[X(l)];
    l += K;
  }
  bool w = 1;
  while (l <= r && ((l - 1) % K != 0 || w)) {
    if (a[l] != 1000000000) S++;
    w = 0;
    if (mn == a[l]) {
      s[X(l)]--;
      return l;
    }
    l++;
  }
  l = 1;
  r = x - 1;
  while (l + K - 1 <= r) {
    if (mn == f[X(l)]) break;
    S += s[X(l)];
    l += K;
  }
  w = 1;
  while (l <= r && ((l - 1) % K != 0 || w)) {
    if (a[l] != 1000000000) S++;
    w = 0;
    if (mn == a[l]) {
      s[X(l)]--;
      return l;
    }
    l++;
  }
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]);
  K = sqrt(n);
  memset(f, 0x3f3f3f, sizeof(f));
  for (int i = 1; i <= n; i++)
    f[(i - 1) / K + 1] = min(f[(i - 1) / K + 1], a[i]);
  for (int i = 1; i <= n; i++) s[(i - 1) / K + 1]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long tmp;
    int x = find(la, tmp);
    ans += tmp;
    la = modify(x);
  }
  printf("%I64d\n", ans);
  return 0;
}
