#include <bits/stdc++.h>
using namespace std;
const pair<int, int> queen[8] = {pair<int, int>(-1, 0), pair<int, int>(-1, 1),
                                 pair<int, int>(0, 1),  pair<int, int>(1, 1),
                                 pair<int, int>(1, 0),  pair<int, int>(1, -1),
                                 pair<int, int>(0, -1), pair<int, int>(-1, -1)};
const int maxN = 1e5 + 11;
int N, A[maxN], BIT[maxN];
vector<int> pos[maxN];
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
  return pair<int, int>(a.first + b.first, a.second + b.second);
}
void update(int x, int v) {
  for (int i = x; i <= N; i += i & (i ^ (i - 1))) BIT[i] += v;
}
int get_BIT(int x) {
  int res = 0;
  for (int i = x; i >= 1; i -= i & (i ^ (i - 1))) res += BIT[i];
  return res;
}
int get_BIT(int l, int r) { return get_BIT(r) - get_BIT(l - 1); }
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> A[i], pos[A[i]].push_back(i);
  long long ans = 0;
  for (int i = 1; i <= N; i++) update(i, 1);
  for (int i = 1, p = 1; i <= 1e5; i++) {
    if (pos[i].empty()) continue;
    int q = lower_bound(pos[i].begin(), pos[i].end(), p) - pos[i].begin();
    for (int j = 0; j < (int)pos[i].size(); j++) {
      q %= (int)pos[i].size();
      if (p <= pos[i][q])
        ans += get_BIT(p, pos[i][q]);
      else
        ans += get_BIT(1, pos[i][q]) + get_BIT(p, N);
      update(pos[i][q], -1);
      p = pos[i][q];
      q++;
    }
  }
  cout << ans;
}
