#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn& a) {
  tn x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  a = x * f;
}
template <typename tn>
void cmax(tn& a, tn b) {
  if (b > a) a = b;
}
template <typename tn>
void cmin(tn& a, tn b) {
  if (b < a) a = b;
}
template <typename tn>
tn SQR(tn x) {
  return x * x;
}
template <typename tn>
tn ABS(tn x) {
  return x < 0 ? -x : x;
}
const int N = 110000;
struct Node {
  int x, y, z;
} b[N];
vector<int> pos[N];
int p[N], a[N], c[N];
int n, m;
long long ans;
bool cmp_x(Node a, Node b) { return a.x < b.x; }
bool cmp_z(Node a, Node b) { return a.z < b.z; }
int lowbit(int x) { return x & (-x); }
void update(int x, int val) {
  while (x <= n) {
    c[x] += val;
    x += lowbit(x);
  }
}
int query(int x) {
  int sum = 0;
  while (x > 0) {
    sum += c[x];
    x -= lowbit(x);
  }
  return sum;
}
void Init() {
  read(n);
  for (int i = (1); i <= (n); i++) {
    read(b[i].x);
    b[i].z = i;
  }
  sort(b + 1, b + n + 1, cmp_x);
  b[1].y = 1;
  for (int i = (2); i <= (n); i++)
    b[i].y = b[i].x == b[i - 1].x ? b[i - 1].y : b[i - 1].y + 1;
  m = b[n].y;
  sort(b + 1, b + n + 1, cmp_z);
  for (int i = (1); i <= (n); i++) {
    a[i] = b[i].y;
    pos[a[i]].push_back(i);
  }
  for (int i = (1); i <= (n); i++) update(i, 1);
}
void Prework() {
  memset(p, 0, sizeof(p));
  p[1] = pos[1][0];
  int num = pos[1].size();
  int L = pos[1][0], R = pos[1][num - 1];
  ans += R;
  for (int i = (0); i <= (num - 1); i++) update(pos[1][i], -1);
  if (m == 1) return;
  vector<int>::iterator nxt = lower_bound(pos[2].begin(), pos[2].end(), R);
  if (nxt != pos[2].end())
    p[2] = *nxt;
  else
    p[2] = pos[2][0];
  if (p[2] > R)
    ans += query(p[2] - 1) - query(R);
  else
    ans += query(n) - query(R) + query(p[2] - 1);
}
void Calc() {
  p[1] = pos[1][0];
  for (int i = (1); i <= (m); i++) {
    int num = pos[i].size(), L = p[i], R;
    if (num == 1)
      R = L;
    else if (L == pos[i][0])
      R = pos[i][num - 1];
    else {
      vector<int>::iterator itR = lower_bound(pos[i].begin(), pos[i].end(), L);
      R = *(--itR);
    }
    if (i == 1)
      ans += R;
    else if (R >= L)
      ans += query(R) - query(L - 1);
    else
      ans += query(n) - query(L - 1) + query(R);
    for (int j = (0); j <= (num - 1); j++) update(pos[i][j], -1);
    if (i != m) {
      vector<int>::iterator nxt =
          lower_bound(pos[i + 1].begin(), pos[i + 1].end(), R);
      if (nxt != pos[i + 1].end())
        p[i + 1] = *nxt;
      else
        p[i + 1] = pos[i + 1][0];
      if (p[i + 1] > R)
        ans += query(p[i + 1] - 1) - query(R);
      else
        ans += query(n) - query(R) + query(p[i + 1] - 1);
    }
  }
  cout << ans << endl;
}
int main() {
  Init();
  Calc();
  return 0;
}
