#include <bits/stdc++.h>
using namespace std;
int n, a[100005];
int num[400004];
void merg(int x) { num[x] = num[2 * x] + num[2 * x + 1]; }
void build(int x = 1, int l = 1, int r = n) {
  if (l == r) {
    num[x] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * x, l, mid);
  build(2 * x + 1, mid + 1, r);
  merg(x);
}
int ind, st, en;
void delt(int x = 1, int l = 1, int r = n) {
  if (l > ind || r < ind) return;
  if (l == r) {
    num[x] = 0;
    return;
  }
  int mid = (l + r) / 2;
  delt(2 * x, l, mid);
  delt(2 * x + 1, mid + 1, r);
  merg(x);
}
int sum(int x = 1, int l = 1, int r = n) {
  if (l > en || r < st) return 0;
  if (l >= st && r <= en) return num[x];
  int mid = (l + r) / 2;
  return sum(2 * x, l, mid) + sum(2 * x + 1, mid + 1, r);
}
vector<int> adj[100004];
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    adj[a[i]].push_back(i);
  }
  build();
  long long ans = 0;
  int now = 1;
  for (int i = 0; i <= 1e5; ++i) {
    if (adj[i].empty()) continue;
    int j = lower_bound(adj[i].begin(), adj[i].end(), now) - adj[i].begin();
    for (int k = j; k < adj[i].size(); ++k) {
      st = now;
      en = adj[i][k];
      ans += sum();
      ind = en;
      delt();
      now = en;
    }
    if (j > 0) {
      st = now;
      en = n;
      ans += sum();
      now = 1;
    }
    for (int k = 0; k < j; ++k) {
      st = now;
      en = adj[i][k];
      ans += sum();
      ind = en;
      delt();
      now = en;
    }
  }
  cout << ans << endl;
}
