#include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 1e5 + 500;
long long a[maxn];
long long fen[maxn];
set<pair<long long, long long> > st;
void update(long long x, long long val) {
  x++;
  while (x < maxn) {
    fen[x] += val;
    x += (x & (-x));
  }
}
long long par(long long x) {
  if (x == 0) return 0;
  long long ans = 0;
  x++;
  while (x > 0) {
    ans += fen[x];
    x -= (x & (-x));
  }
  return ans;
}
pair<long long, long long> next(long long p1) {
  pair<long long, long long> o = (*(st.begin()));
  set<pair<long long, long long> >::iterator it;
  it = st.lower_bound(make_pair(o.first, p1));
  if (it != st.end()) {
    pair<long long, long long> p = (*it);
    if (p.first == o.first) return p;
    return o;
  }
  return o;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    update(i, 1);
    cin >> a[i];
    st.insert(make_pair(a[i], i));
  }
  long long p1 = 0;
  long long ans = 0;
  while (!st.empty()) {
    pair<long long, long long> v = next(p1);
    st.erase(v);
    if (v.second < p1) {
      ans += par(n) - par(p1) + par(v.second);
      p1 = v.second;
    } else {
      ans += par(v.second) - par(p1);
      p1 = v.second;
    }
    update(v.second, -1);
  }
  cout << ans;
}
