#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a) out << it << " ";
  return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a) in >> i;
  return in;
}
ifstream cinn("in.txt");
ofstream coutt("out.txt");
long long poww(const long long &a, long long b,
               const long long &m = 1000000007) {
  if (b == 0) return 1;
  long long first = poww(a, b / 2, m);
  first = first * first % m;
  if (b & 1) first = first * a % m;
  return first;
}
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long rint(long long l, long long r) { return rand() % (r - l + 1) + l; }
struct bit {
  long long n;
  vector<long long> B;
  void began(long long a) {
    n = a + 1;
    long long i;
    for (i = 0; i < n + 5; i++) {
      B.push_back(0);
    }
  }
  void update(long long p, long long val) {
    while (p <= n) {
      B[p] += val;
      p += p & (-p);
    }
  }
  long long sum(long long p) {
    long long s = 0;
    if (p == 0) return 0;
    while (p > 0) {
      s += B[p];
      p -= p & (-p);
    }
    return s;
  }
};
set<long long> graph[100005];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, first, t;
  string s1, s2;
  vector<pair<long long, long long> > v;
  long long n;
  cin >> n;
  bit tree;
  tree.began(n + 1);
  for (i = 0; i < n; i++) {
    cin >> first;
    v.push_back({first, i + 1});
    graph[first].insert(i + 1);
    tree.update(i + 1, 1);
  }
  sort((v).begin(), (v).end());
  long long cur = 1, ans = 0;
  for (j = 0; j < 100005; j++) {
    while (graph[j].size() > 0) {
      auto it = graph[j].lower_bound(cur);
      if (it == graph[j].end()) {
        first = *graph[j].begin();
        graph[j].erase(graph[j].begin());
      } else {
        first = *it;
        graph[j].erase(it);
      }
      if (first >= cur) {
        long long a = tree.sum(first) - tree.sum(cur - 1);
        tree.update(first, -1);
        cur = first + 1;
        ans += a;
        if (cur == n + 1) {
          cur = 1;
        }
      } else {
        ans += tree.sum(n) - tree.sum(cur - 1) + tree.sum(first);
        cur = first + 1;
        tree.update(first, -1);
        if (cur == n + 1) {
          cur = 1;
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
