#include <bits/stdc++.h>
using namespace std;
long long add[800005] = {0};
long long val[800005] = {0};
void apply(long long x, long long l, long long r, long long v) {
  val[x] += v * (long long)(r - l + 1);
  add[x] += v;
}
void pushdown(long long x, long long l, long long r) {
  if (!add[x]) return;
  apply((x << 1), l, ((l + r) >> 1), add[x]);
  apply(((x << 1) | 1), ((l + r) >> 1) + 1, r, add[x]);
  add[x] = 0;
}
void modify(long long x, long long l, long long r, long long L, long long R,
            long long v) {
  if (L <= l && r <= R) {
    apply(x, l, r, v);
    return;
  }
  pushdown(x, l, r);
  if (L <= ((l + r) >> 1)) modify((x << 1), l, ((l + r) >> 1), L, R, v);
  if (R > ((l + r) >> 1))
    modify(((x << 1) | 1), ((l + r) >> 1) + 1, r, L, R, v);
  val[x] = val[(x << 1)] + val[((x << 1) | 1)];
}
long long query(long long x, long long l, long long r, long long L,
                long long R) {
  if (L > R) return 0;
  if (L <= l && r <= R) return val[x];
  long long ret = 0;
  pushdown(x, l, r);
  if (L <= ((l + r) >> 1)) ret += query((x << 1), l, ((l + r) >> 1), L, R);
  if (R > ((l + r) >> 1))
    ret += query(((x << 1) | 1), ((l + r) >> 1) + 1, r, L, R);
  val[x] = val[(x << 1)] + val[((x << 1) | 1)];
  return ret;
}
long long n, p, ans = 0;
long long a[100005] = {0};
vector<long long> pos[100005];
int main() {
  cin >> n;
  modify(1, 1, n, 1, n, 1);
  for (long long i = 1; i <= n; i++) {
    cin >> p;
    pos[p].push_back(i);
  }
  p = 1;
  for (long long i = 1; i <= 100000; i++) {
    if (!pos[i].size()) continue;
    long long to = pos[i].size() - 1;
    for (long long j = 0; j < pos[i].size(); j++) {
      if (pos[i][j] < p)
        to = j;
      else
        break;
    }
    if (p > pos[i][to]) {
      ans += query(1, 1, n, p, n);
      p = 1;
    }
    ans += query(1, 1, n, p, pos[i][to]);
    for (long long j = 0; j < pos[i].size(); j++) {
      modify(1, 1, n, pos[i][j], pos[i][j], -1);
    }
    p = pos[i][to] + 1;
  }
  cout << ans << endl;
  return 0;
}
