#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int tr[4 * N];
void build(int node, int b, int e) {
  if (b == e) {
    tr[node] = 1;
  } else {
    int mid = (b + e) / 2;
    build(2 * node, b, mid);
    build(2 * node + 1, mid + 1, e);
    tr[node] = tr[2 * node] + tr[2 * node + 1];
  }
}
int query(int node, int b, int e, int i, int j) {
  if (e < i or j < b) return 0;
  if (i <= b and j >= e) return tr[node];
  int mid = (b + e) / 2;
  return query(2 * node, b, mid, i, j) + query(2 * node + 1, mid + 1, e, i, j);
}
void update(int node, int b, int e, int i) {
  if (e < i or b > i) return;
  if (b == e) {
    tr[node] = 0;
    return;
  }
  int mid = (b + e) / 2;
  update(2 * node, b, mid, i);
  update(2 * node + 1, mid + 1, e, i);
  tr[node] = tr[2 * node] + tr[2 * node + 1];
}
int n;
int v[N];
set<int> pos[N];
long long int resp = 0;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    pos[v[i]].insert(i);
  }
  sort(v, v + n);
  build(1, 0, n - 1);
  int last = 0;
  vector<int> hist;
  for (int i = 0; i < n; i++) {
    auto it = pos[v[i]].lower_bound(last);
    if (it != pos[v[i]].end()) {
      last = *it;
      hist.push_back(last);
      pos[v[i]].erase(it);
    } else {
      resp += query(1, 0, n - 1, 0, last);
      for (int x : hist) update(1, 0, n - 1, x);
      int aux = last;
      last = *pos[v[i]].begin();
      pos[v[i]].erase(pos[v[i]].begin());
      resp += query(1, 0, n - 1, aux, n - 1);
      hist.clear();
      hist.push_back(last);
    }
  }
  resp += query(1, 0, n - 1, 0, n - 1);
  cout << resp << endl;
  return 0;
}
