#include <bits/stdc++.h>
using namespace std;
void init(int l, int r, vector<int> &Tree) {
  if (r - l > 1) {
    init(l, (l + r) / 2, Tree);
    init((l + r) / 2, r, Tree);
    Tree[l + r] = Tree[l + (l + r) / 2] + Tree[(l + r) / 2 + r];
  }
}
void update(int l, int r, int b, int e, int v, vector<int> &Tree) {
  if (e <= l || r <= b) {
  } else if (l <= b && e <= r)
    Tree[b + e] += v;
  else {
    update(l, r, b, (b + e) / 2, v, Tree);
    update(l, r, (b + e) / 2, e, v, Tree);
    Tree[b + e] = Tree[b + (b + e) / 2] + Tree[(b + e) / 2 + e];
  }
}
int query(int l, int r, int b, int e, vector<int> &Tree) {
  if (e <= l || r <= b)
    return 0;
  else if (l <= b && e <= r)
    return Tree[b + e];
  else
    return query(l, r, b, (b + e) / 2, Tree) +
           query(l, r, (b + e) / 2, e, Tree);
}
int main() {
  int n, a, s = (1 << 18), las = 0;
  long long ans = 0;
  scanf("%d", &n);
  vector<int> Tree(s, 0);
  vector<vector<int> > Pos(100001);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    Pos[a].push_back(i);
  }
  for (int i = 1; i <= n; i++) Tree[2 * i + 1] = 1;
  init(0, s / 2, Tree);
  for (int v = 1; v <= 100000; v++)
    if (!Pos[v].empty()) {
      if (Pos[v].size() == 1) {
        if (las < Pos[v][0])
          ans += query(las + 1, Pos[v][0] + 1, 0, s / 2, Tree);
        else
          ans += (query(las + 1, s / 2, 0, s / 2, Tree) +
                  query(0, Pos[v][0] + 1, 0, s / 2, Tree));
        update(Pos[v][0], Pos[v][0] + 1, 0, s / 2, -1, Tree);
        las = Pos[v][0];
      } else {
        int j = 0, s1 = Pos[v].size();
        while (j < s1 && Pos[v][j] < las) j++;
        if (j != s1)
          ans += query(las + 1, Pos[v][j] + 1, 0, s / 2, Tree);
        else {
          ans += (query(las + 1, s / 2, 0, s / 2, Tree) +
                  query(0, Pos[v][0] + 1, 0, s / 2, Tree));
          j = 0;
        }
        for (int k = j; k < s1 - 1; k++)
          ans += query(Pos[v][k] + 1, Pos[v][k + 1] + 1, 0, s / 2, Tree);
        if (j != 0) {
          ans += (query(Pos[v][s1 - 1] + 1, s / 2, 0, s / 2, Tree) +
                  query(0, Pos[v][0] + 1, 0, s / 2, Tree));
          for (int k = 0; k < j - 1; k++)
            ans += query(Pos[v][k] + 1, Pos[v][k + 1] + 1, 0, s / 2, Tree);
        }
        for (int k = 0; k < s1; k++)
          update(Pos[v][k], Pos[v][k] + 1, 0, s / 2, -1, Tree);
        las = Pos[v][(j + s1 - 1) % s1];
      }
    }
  printf("%I64d\n", ans);
  return 0;
}
