#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5;
int n;
vector<int> pos[NMAX + 1];
set<int> numbers;
int bit[NMAX + 1];
void update(int i) {
  int val = i + 1;
  while (val <= n) {
    bit[val]--;
    val += val & (~val + 1);
  }
}
int query(int i) {
  int val = i + 1, ans = 0;
  while (val > 0) {
    ans += bit[val];
    val -= val & (~val + 1);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    numbers.insert(temp);
    pos[temp].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    int val = i + 1;
    while (val <= n) {
      bit[val]++;
      val += val & (~val + 1);
    }
  }
  long long ans = 0;
  int last = -1;
  for (int num : numbers) {
    if (0) cout << num << ":  \n";
    if (0) cout << "last: " << last << '\n';
    if (pos[num][0] > last) {
      int next = pos[num][pos[num].size() - 1];
      if (0) cout << query(last - 1) << '\n';
      ans += query(next) - query(last - 1);
      last = next;
    } else {
      int p;
      for (int i = pos[num].size() - 1; i >= 0; i--) {
        if (pos[num][i] < last) {
          p = pos[num][i];
          break;
        }
      }
      ans += query(p) + query(n - 1) - query(last - 1);
      if (0) cout << query(p) + query(n - 1) - query(last - 1) << '\n';
      last = p;
    }
    for (int p : pos[num]) {
      update(p);
    }
  }
  cout << ans << '\n';
  return 0;
}
