#include <bits/stdc++.h>
using std::vector;
vector<int64_t> fenwick_tree;
int64_t sum(int64_t r) {
  int64_t res = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) {
    res += fenwick_tree[r];
  }
  return res;
}
void inc(int64_t i, int64_t delta) {
  for (; i < int64_t(fenwick_tree.size()); i = (i | (i + 1))) {
    fenwick_tree[i] += delta;
  }
}
int64_t sum(int64_t l, int64_t r) { return sum(r) - sum(l - 1); }
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int64_t n;
  std::cin >> n;
  vector<int64_t> cards(n);
  for (auto& elem : cards) {
    std::cin >> elem;
  }
  fenwick_tree.resize(n, 0);
  std::map<int64_t, vector<int64_t>> data;
  for (size_t i = 0; i < n; ++i) {
    inc(i, 1);
    data[cards[i]].push_back(i);
  }
  size_t cur_pos = 0;
  int64_t res = 0;
  for (auto elem : data) {
    auto pos = elem.second;
    int64_t it = 0;
    while (it < pos.size() && pos[it] < cur_pos) {
      ++it;
    }
    int64_t cnt = pos.size();
    while (cnt) {
      if (it == pos.size()) {
        it = 0;
      }
      if (cur_pos <= pos[it]) {
        res += sum(cur_pos, pos[it]);
      } else {
        res += sum(cur_pos, n - 1) + sum(0, pos[it]);
      }
      cur_pos = pos[it];
      inc(cur_pos, -1);
      ++it, --cnt;
    }
  }
  std::cout << res;
  return 0;
}
