#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
vector<int> A[N];
int c[N];
void add(int x, int y, int n) {
  for (; x <= n; x += x & -x) {
    c[x] += y;
  }
}
int query(int x) {
  int ret = 0;
  for (; x; x -= x & -x) {
    ret += c[x];
  }
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    A[a[i]].push_back(i);
  }
  vector<int> ord;
  int cur = 0;
  for (int i = 1; i < N; ++i) {
    if (A[i].empty()) continue;
    auto it = lower_bound(A[i].begin(), A[i].end(), cur);
    for (auto it2 = it; it2 != A[i].end(); ++it2) {
      ord.push_back(*it2 + 1);
      cur = *it2;
    }
    for (auto it2 = A[i].begin(); it2 != it; ++it2) {
      ord.push_back(*it2 + 1);
      cur = *it2;
    }
  }
  for (int i = 0; i < n; ++i) add(i + 1, 1, n);
  long long ret = 0;
  int pre = 0;
  for (auto& cur : ord) {
    if (pre < cur) {
      ret += query(cur) - query(pre);
    } else {
      ret += query(n) - query(pre) + query(cur);
    }
    add(cur, -1, n);
    pre = cur;
  }
  printf("%lld\n", ret);
  return 0;
}
