#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
vector<int> node[N];
int vec[N];
int n;
int getSum(int index) {
  if (index < 0) return 0;
  int sum = 0;
  index = index + 1;
  while (index > 0) {
    sum += vec[index];
    index -= index & (-index);
  }
  return sum;
}
void updateBIT(int index, int val) {
  index = index + 1;
  while (index <= n) {
    vec[index] += val;
    index += index & (-index);
  }
}
int32_t main() {
  cin >> n;
  vector<pair<int, int>> arr(n);
  for (int i = 0; i < n; ++i) {
    int elem;
    cin >> elem;
    arr[i] = {elem, i};
    updateBIT(i, 1);
    node[elem].push_back(i);
  }
  sort(arr.begin(), arr.end());
  long long ans = 0;
  int prev = -1;
  int elm = -1;
  int j = -1;
  for (int i = 0; i < n; ++i) {
    int idx = arr[i].second;
    int elem = arr[i].first;
    if (elem == elm) {
      j++;
      j = j % ((int)node[elem].size());
      idx = node[elem][j];
    } else {
      j = upper_bound(node[elem].begin(), node[elem].end(), prev) -
          node[elem].begin();
      j = j % ((int)node[elem].size());
      idx = node[elem][j];
      elm = elem;
    }
    if (idx > prev)
      ans = ans + (getSum(idx) - getSum(prev));
    else
      ans = ans + getSum(idx) + (getSum(n - 1) - getSum(prev));
    updateBIT(idx, -1);
    prev = idx;
  }
  cout << ans;
  return 0;
}
