#include <bits/stdc++.h>
using namespace std;
const int64_t mn = int64_t(1e5) + 10;
vector<int64_t> d[mn];
int64_t f[mn];
int64_t n;
void docfile() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  if (ifstream("test"
               ".inp")) {
    freopen(
        "test"
        ".inp",
        "r", stdin);
    freopen(
        "test"
        ".out",
        "w", stdout);
  } else if (ifstream(""
                      ".inp")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
}
class mycomp {
 public:
  bool ok;
  mycomp(bool ok1 = false) { ok = ok1; }
  bool operator()(pair<int64_t, int64_t> i, pair<int64_t, int64_t> j) const {
    if (i.first != j.first) return (j.first < i.first);
    return (j.second > i.second);
  }
};
void enter() {}
void init() {}
void ub(int64_t x) {
  for (int64_t i = x; i <= n; i += i & -i) ++f[i];
}
int64_t db(int64_t x) {
  int64_t s = 0;
  for (int64_t i = x; i >= 1; i -= i & -i) s += f[i];
  return s;
}
void solve() {
  set<int64_t> mu;
  cin >> n;
  for (int64_t i = n; i >= 1; --i) {
    int64_t x;
    cin >> x;
    d[x].push_back(i);
    mu.insert(x);
  }
  for (int64_t i = 1; i <= 100000; ++i) sort(d[i].begin(), d[i].end());
  int64_t res = n + 1;
  int64_t sol = 0;
  for (set<int64_t>::iterator it = mu.begin(); it != mu.end(); ++it) {
    int64_t vt = int64_t(lower_bound(d[*it].begin(), d[*it].end(), res) -
                         d[*it].begin());
    vt--;
    if (vt < 0) vt = d[*it].size() - 1;
    for (int64_t i = vt; i >= 0; --i) {
      int64_t tm = d[*it][i];
      if (res >= tm)
        sol += (res - tm - 1 - (db(res - 1) - db(tm)));
      else
        sol += res - 1 + n - tm - (db(res - 1) + db(n) - db(tm));
      ub(tm);
      res = tm;
    }
    for (int64_t i = d[*it].size() - 1; i >= vt + 1; --i) {
      int64_t tm = d[*it][i];
      if (res >= tm)
        sol += (res - tm - 1 - (db(res - 1) - db(tm)));
      else
        sol += res - 1 + n - tm - (db(res - 1) + db(n) - db(tm));
      ub(tm);
      res = tm;
    }
  }
  cout << sol + n;
}
void print_result() {}
int32_t main() {
  docfile();
  int64_t t = 1;
  for (int64_t tt = 0; tt < t; ++tt) {
    enter();
    init();
    solve();
    print_result();
  }
}
