#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long n, arr[100005], seg[400005], ans, lst;
map<long long, vector<long long>> mp;
void build(long long node, long long s, long long e) {
  if (s == e) {
    seg[node] = 1;
    return;
  }
  long long mid = s + (e - s) / 2;
  build(node * 2, s, mid);
  build(node * 2 + 1, mid + 1, e);
  seg[node] = seg[node * 2] + seg[node * 2 + 1];
}
void update(long long node, long long s, long long e, long long ind) {
  if (s == e) {
    seg[node] = 0;
    return;
  }
  long long mid = s + (e - s) / 2;
  if (mid >= ind)
    update(node * 2, s, mid, ind);
  else
    update(node * 2 + 1, mid + 1, e, ind);
  seg[node] = seg[node * 2] + seg[node * 2 + 1];
}
long long query(long long node, long long s, long long e, long long l,
                long long r) {
  if (e < l || s > r) return 0;
  if (s >= l && e <= r) return seg[node];
  long long mid = s + (e - s) / 2;
  return query(node * 2, s, mid, l, r) + query(node * 2 + 1, mid + 1, e, l, r);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i], mp[arr[i]].push_back(i);
  build(1, 0, n - 1);
  for (auto i : mp) {
    if (lst <= i.second[0]) {
      ans += query(1, 0, n - 1, lst, i.second.back());
      lst = i.second.back();
    } else {
      long long low = 0, high = i.second.size() - 1, mid, ind;
      while (low <= high) {
        mid = low + (high - low) / 2;
        if (i.second[mid] < lst)
          ind = i.second[mid], low = mid + 1;
        else
          high = mid - 1;
      }
      ans += query(1, 0, n - 1, lst, n - 1) + query(1, 0, n - 1, 0, ind);
      lst = ind;
    }
    for (auto j : i.second) update(1, 0, n - 1, j);
  }
  cout << ans;
  return 0;
}
