#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, niz[MAXN], tree[MAXN], curr, current;
long long r;
vector<int> v[100010], ve;
bool b[100010];
int query(int a) {
  if (a == 0) {
    return 0;
  }
  return tree[a] + query(a - (a & -a));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> niz[i];
    v[niz[i]].push_back(i + 1);
    b[niz[i]] = 1;
  }
  for (int i = 1; i <= 100010; ++i) {
    if (b[i]) {
      ve.push_back(i);
    }
  }
  for (int i = 0; i < ve.size(); ++i) {
    curr = ve[i];
    if (current <= v[curr][0]) {
      r += v[curr][v[curr].size() - 1] - current -
           (query(v[curr][v[curr].size() - 1]) - query(current));
      current = v[curr][v[curr].size() - 1];
    } else {
      int j = *--lower_bound(v[curr].begin(), v[curr].end(), current);
      r += n - (current - j) - (query(n) - query(current)) - query(j);
      current = j;
    }
    for (int j = 0; j < v[curr].size(); ++j) {
      for (int k = v[curr][j]; k <= n; k += (k & -k)) {
        ++tree[k];
      }
    }
  }
  cout << r;
  return 0;
}
