#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int INF = 1e9;
int sz = 1;
struct SegmentTree {
  const long long MMN = 0;
  const long long MMX = 1e18;
  struct Node {
    long long v;
    vector<pair<char, long long>> pir;
    Node() {}
    Node(long long v) : v(v) {}
  };
  vector<Node> tr;
  void init(int n, vector<long long>& b) {
    while (sz < n) {
      sz *= 2;
    }
    tr.assign(2 * sz, Node(0));
    for (int i = sz; i < sz + n; i++) {
      tr[i].v = b[i - sz];
    }
    for (int i = sz - 1; i > 0; i--) {
      tr[i].v = tr[i * 2].v + tr[i * 2 + 1].v;
    }
  };
  void push(int x, int l, int r) {
    for (int i = 0; i < tr[x].pir.size(); i++) {
      long long v = tr[x].pir[i].second * (r - l + 1);
      if (tr[x].pir[i].first == 0) {
        tr[x].v = tr[x].pir[i].second;
        if (x * 2 < tr.size()) {
          tr[x * 2].pir.clear();
          tr[x * 2 + 1].pir.clear();
          tr[x * 2].pir.push_back(make_pair(0, tr[x].pir[i].second));
          tr[x * 2 + 1].pir.push_back(make_pair(0, tr[x].pir[i].second));
        }
      } else {
        tr[x].v += tr[x].pir[i].second * (r - l + 1);
        if (x * 2 < tr.size()) {
          if (tr[x * 2].pir.empty())
            tr[x * 2].pir.push_back(make_pair(1, v));
          else
            tr[x * 2].pir.back().second += v;
          if (tr[x * 2 + 1].pir.empty())
            tr[x * 2 + 1].pir.push_back(make_pair(1, v));
          else
            tr[x * 2 + 1].pir.back().second += v;
        }
      }
    }
    tr[x].pir.clear();
  }
  long long Sum(int a = -1, int b = -1, int x = 1, int l = sz,
                int r = sz * 2 - 1) {
    push(x, l, r);
    if (a > r || b < l) return 0;
    if (a <= l && b >= r) {
      return tr[x].v;
    }
    long long s1 = Sum(a, b, x * 2, l, (l + r) / 2);
    long long s2 = Sum(a, b, x * 2 + 1, (l + r) / 2 + 1, r);
    return s1 + s2;
  }
  void Set(long long v, int a = -1, int b = -1, int x = 1, int l = sz,
           int r = sz * 2 - 1) {
    push(x, l, r);
    if (a > r || b < l) return;
    if (a <= l && b >= r) {
      tr[x].v = v * (r - l + 1);
      if (x * 2 < tr.size()) {
        tr[x * 2].pir.clear();
        tr[x * 2 + 1].pir.clear();
        tr[x * 2].pir.push_back(make_pair(0, v));
        tr[x * 2 + 1].pir.push_back(make_pair(0, v));
      }
      return;
    }
    Set(v, a, b, x * 2, l, (l + r) / 2);
    Set(v, a, b, x * 2 + 1, (l + r) / 2 + 1, r);
    tr[x].v = tr[x * 2].v + tr[x * 2 + 1].v;
  }
  void Add(long long v, int a = -1, int b = -1, int x = 1, int l = sz,
           int r = sz * 2 - 1) {
    push(x, l, r);
    if (a > r || b < l) return;
    if (a <= l && b >= r) {
      tr[x].v += v * (r - l + 1);
      if (x * 2 < tr.size()) {
        if (tr[x * 2].pir.empty())
          tr[x * 2].pir.push_back(make_pair(1, v));
        else
          tr[x * 2].pir.back().second += v;
        if (tr[x * 2 + 1].pir.empty())
          tr[x * 2 + 1].pir.push_back(make_pair(1, v));
        else
          tr[x * 2 + 1].pir.back().second += v;
      }
      return;
    }
    Add(v, a, b, x * 2, l, (l + r) / 2);
    Add(v, a, b, x * 2 + 1, (l + r) / 2 + 1, r);
    tr[x].v = tr[x * 2].v + tr[x * 2 + 1].v;
  }
};
int main() {
  int n;
  cin >> n;
  SegmentTree tree;
  vector<long long> w(n, 1);
  tree.init(n, w);
  map<int, set<int>> v;
  vector<int> v1(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v[x].insert(i);
    v1[i] = x;
  }
  sort(v1.begin(), v1.end());
  int l = 0;
  long long sum = 0;
  for (int q : v1) {
    auto rr = v[q].begin();
    auto rr1 = v[q].lower_bound(l);
    if (rr1 != v[q].end() && *rr < l) {
      rr = rr1;
    }
    int r = *rr;
    if (r < l) {
      sum += tree.Sum(l + sz, v1.size() + sz);
      sum += tree.Sum(sz, r + sz);
      tree.Set(0, r + sz, r + sz);
    } else {
      sum += tree.Sum(l + sz, r + sz);
      tree.Set(0, r + sz, r + sz);
    }
    v[q].erase(rr);
    l = r;
  }
  cout << sum << endl;
  return 0;
}
