#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
void solve() {
  int n, x, y, t, c = 0;
  cin >> n;
  vector<int> v[100009];
  for (int i = 0; i < n; i++) {
    cin >> x;
    v[x].push_back(i);
  }
  int max1 = -1;
  long long ans = 0;
  for (int i = 0; i < 100001; i++) {
    if (v[i].size() == 0) {
      continue;
    } else if (v[i][0] > max1) {
      max1 = (v[i].back());
      c += v[i].size();
    } else {
      int idx = lower_bound(v[i].begin(), v[i].end(), max1) - v[i].begin();
      c += v[i].size() - idx;
      ans += n;
      n -= c;
      c = 0;
      max1 = v[i][idx - 1];
      c += idx;
    }
  }
  cout << ans + n;
}
