#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:216000000")
using namespace std;
const long long MAX = 100000000LL * 100000000LL;
const long long MIN = numeric_limits<long long>::min();
const double PI = 3.14159265358979;
const long long MOD = 1000000007LL;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (int i = 0; i < v.size(); ++i) out << v[i] << " ";
  return out;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
  for (int i = 0; i < v.size(); ++i) in >> v[i];
  return in;
}
template <class L, class R>
istream& operator>>(istream& in, pair<L, R>& p) {
  in >> p.first >> p.second;
  return in;
}
template <class T>
T lexical_cast(string& s) {
  stringstream ss(s);
  T t;
  ss >> t;
  return t;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long cdiv(long long a, long long b) {
  return (a % b) ? (a / b + 1) : (a / b);
}
long long inv(long long n, long long mod) {
  long long pow = mod - 2;
  long long ans = 1;
  long long cur = n;
  while (pow > 0) {
    if (pow & 1) {
      ans *= cur;
      ans %= mod;
    }
    pow /= 2;
    cur *= cur;
    cur %= mod;
  }
  return ans;
}
template <class Cont>
void sort(Cont& c) {
  sort(begin(c), end(c));
}
template <class Cont>
void reverse(Cont& c) {
  reverse(begin(c), end(c));
}
struct segtree {
  int N;
  vector<int> data;
  segtree(int n) {
    N = n;
    data.resize(2 * N);
  }
  void set(int p, int value) {
    for (data[p += N] = value; p > 1; p >>= 1) {
      data[p >> 1] = data[p] + data[p ^ 1];
    }
  }
  int query(int l, int r) {
    int ans = 0;
    for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        ans += data[l++];
      }
      if (r & 1) {
        ans += data[--r];
      }
    }
    return ans;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  segtree tree(n);
  for (int i = 0; i < n; ++i) {
    tree.set(i, 1);
  }
  vector<vector<int>> pos(1000001);
  for (int i = 0; i < n; ++i) {
    pos[a[i]].push_back(i);
  }
  long long ans = 0;
  int lastpos = 0;
  int index = 0;
  while (index < 1000001) {
    if (pos[index].size() > 0) {
      auto first = lower_bound(pos[index].begin(), pos[index].end(), lastpos);
      auto it = first;
      while (it != pos[index].end()) {
        ans += tree.query(lastpos, (*it) + 1);
        tree.set(*it, 0);
        lastpos = *it;
        ++it;
      }
      it = pos[index].begin();
      while (it != first) {
        if (lastpos > *it) {
          ans += tree.query(lastpos, n);
          ans += tree.query(0, (*it) + 1);
        } else
          ans += tree.query(lastpos, (*it) + 1);
        tree.set(*it, 0);
        lastpos = *it;
        ++it;
      }
    }
    index++;
  }
  cout << ans;
}
