#include <bits/stdc++.h>
using namespace std;
struct node {
  int x;
  int y;
  int s;
  int mn;
};
int init(int i, vector<node>& tr, vector<int>& v, int x, int y) {
  tr[i].x = x;
  tr[i].y = y;
  tr[i].s = y - x;
  if (y - x == 1) {
    tr[i].mn = x;
    return x;
  }
  int m = (x + y) / 2;
  int a = init(2 * i, tr, v, x, m);
  int b = init(2 * i + 1, tr, v, m, y);
  tr[i].mn = (v[b] < v[a] ? b : a);
  return tr[i].mn;
}
int upd(int i, vector<node>& tr, vector<int>& v, int pos) {
  if (pos < tr[i].x || pos >= tr[i].y) {
    return tr[i].mn;
  }
  --tr[i].s;
  if (tr[i].x + 1 == tr[i].y) {
    v[pos] = 1e9;
    return pos;
  }
  int a = upd(2 * i, tr, v, pos);
  int b = upd(2 * i + 1, tr, v, pos);
  tr[i].mn = (v[b] < v[a] ? b : a);
  return tr[i].mn;
}
int get_min(int i, vector<node>& tr, vector<int>& v, int x, int y) {
  if (tr[i].x >= y || tr[i].y <= x) {
    return -1;
  }
  if (x <= tr[i].x && tr[i].y <= y) {
    return tr[i].mn;
  }
  int a = get_min(2 * i, tr, v, x, y);
  int b = get_min(2 * i + 1, tr, v, x, y);
  if (a == -1) {
    swap(a, b);
  }
  if (a != -1 && b != -1) {
    return (v[b] < v[a] ? b : a);
  }
  return a;
}
int get_sum(int i, vector<node>& tr, int x, int y) {
  if (tr[i].x >= y || tr[i].y <= x) {
    return 0;
  }
  if (x <= tr[i].x && tr[i].y <= y) {
    return tr[i].s;
  }
  return get_sum(2 * i, tr, x, y) + get_sum(2 * i + 1, tr, x, y);
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout.setf(ios_base::fixed);
  cout.precision(28);
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto& x : v) {
    cin >> x;
  }
  for (int i = 0; i < n; ++i) {
    v.push_back(v[i]);
  }
  long long ans = 0;
  vector<node> tr(8 * n + 10);
  init(1, tr, v, 0, 2 * n);
  int c = 0;
  for (int i = 0; i < n; ++i) {
    int x = get_min(1, tr, v, c, 2 * n);
    ans += get_sum(1, tr, c, x + 1);
    x %= n;
    upd(1, tr, v, x);
    upd(1, tr, v, x + n);
    c = x;
  }
  cout << ans << endl;
  return 0;
}
