#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
int n, m, q, minn = 1e9;
vector<int> vec[mx];
long long sum[mx];
inline int lowbit(int x) { return x & (-x); }
void add(int x, int v) {
  for (int i = x; i <= n; i += lowbit(i)) sum[i] += v;
}
long long getsum(int x) {
  long long ans = 0;
  while (x) {
    ans += sum[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  while (~scanf("%d", &n)) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++)
      scanf("%d", &q), vec[q].push_back(i), add(i, 1), minn = min(q, minn);
    long long ans = getsum(vec[minn].back()), k = vec[minn].back();
    for (int j = 0; j < vec[minn].size(); j++) add(vec[minn][j], -1);
    for (int i = minn + 1; i < mx; i++) {
      if (!vec[i].size()) continue;
      auto it = lower_bound(vec[i].begin(), vec[i].end(), k);
      if (it == vec[i].begin())
        ans += getsum(*(it = --vec[i].end())) - getsum(k - 1);
      else
        ans += getsum(*(--it)) + getsum(n) - getsum(k - 1);
      k = *it;
      for (int j = 0; j < vec[i].size(); j++) add(vec[i][j], -1);
    }
    cout << ans << endl;
  }
  return 0;
}
