#include <bits/stdc++.h>
using namespace std;
const int inv = 1000000000;
const int minv = -inv;
struct BIT {
  int bn;
  vector<int> bA;
  BIT() { bn = 0; }
  BIT(int bn_) {
    bn = bn_;
    bA.resize(bn + 1);
    fill(bA.begin(), bA.end(), 0);
  }
  int prefix(int bposn) {
    if (bposn <= 0) return 0;
    if (bposn > bn) bposn = bn;
    int ret = 0;
    for (int i = bposn; i > 0; i -= ((i) & (-i))) ret += bA[i];
    return ret;
  }
  void update(int bposn, int bincr) {
    if (bposn <= 0) return;
    if (bposn > bn) return;
    for (int i = bposn; i <= bn; i += ((i) & (-i))) bA[i] += bincr;
  }
  int query(int bl, int br) {
    if (bl > br) return 0;
    return (prefix(br) - prefix(bl - 1));
  }
};
const int max_n = 100000 + 5;
const int max_a = 100000 + 5;
int n;
int a[max_n];
vector<int> occ[max_a + 1];
BIT bit;
int inc(int i, int m) { return (i == m - 1 ? 0 : i + 1); }
int dec(int i, int m) { return (i == 0 ? m - 1 : i - 1); }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    occ[a[i]].push_back(i);
  }
  bit = BIT(n);
  long long res = 0ll;
  int lc = n - 1;
  for (int i = 1; i <= max_a; ++i)
    if ((int)occ[i].size() > 0) {
      int nocci = (int)occ[i].size();
      int sj = -1;
      for (int j = 0; j < nocci; ++j)
        if (occ[i][j] > lc) {
          sj = j;
          break;
        }
      if (sj != -1)
        res += ((long long)(occ[i][sj] - lc -
                            bit.query(lc + 1 + 1, occ[i][sj] + 1)));
      else {
        sj = 0;
        res += ((long long)(occ[i][sj] + n - lc -
                            bit.query(lc + 1 + 1, n - 1 + 1) -
                            bit.query(0 + 1, occ[i][sj] + 1)));
      }
      lc = occ[i][sj];
      if (nocci > 1) {
        for (int j = inc(sj, nocci); j != sj; j = inc(j, nocci)) {
          if (occ[i][j] > occ[i][dec(j, nocci)])
            res += ((long long)(occ[i][j] - occ[i][dec(j, nocci)] -
                                bit.query(occ[i][dec(j, nocci)] + 1 + 1,
                                          occ[i][j] + 1)));
          else
            res += ((
                long long)(occ[i][j] + n - occ[i][dec(j, nocci)] -
                           bit.query(occ[i][dec(j, nocci)] + 1 + 1, n - 1 + 1) -
                           bit.query(0 + 1, occ[i][j] + 1)));
          lc = occ[i][j];
        }
      }
      for (int j = 0; j < nocci; ++j) bit.update(occ[i][j] + 1, 1);
    }
  cout << res << "\n";
  return 0;
}
