#include <bits/stdc++.h>
using namespace std;
int N, a[(long long)1e5 + 7], sum[(long long)1e5 + 7],
    st[4 * (long long)1e5 + 7], idx[4 * (long long)1e5 + 7],
    lft[(long long)1e5 + 7];
long long ans;
void build(int l, int r, int p) {
  if (l == r) {
    st[p] = sum[l];
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, p * 2);
  ;
  build(mid + 1, r, p * 2 + 1);
  st[p] = st[p * 2] + st[p * 2 + 1];
}
int query(int l, int r, int p, int i, int j) {
  if (i > j) return 0;
  if (r < i || l > j) return 0;
  if (i <= l && j >= r) return st[p];
  int mid = (l + r) / 2;
  int q1 = query(l, mid, p * 2, i, j);
  int q2 = query(mid + 1, r, p * 2 + 1, i, j);
  return q1 + q2;
}
void update(int l, int r, int p, int i, int j) {
  if (r < i || l > j) return;
  if (i <= l && j >= r) {
    st[p]--;
    sum[r] = 0;
    return;
  }
  int mid = (l + r) / 2;
  update(l, mid, p * 2, i, j);
  update(mid + 1, r, p * 2 + 1, i, j);
  st[p] = st[p * 2] + st[p * 2 + 1];
}
void build2(int l, int r, int p) {
  if (l == r) {
    idx[p] = l;
    return;
  }
  int mid = (l + r) / 2, p1 = p * 2, p2 = p * 2 + 1;
  build2(l, mid, p1);
  build2(mid + 1, r, p2);
  if (a[idx[p1]] <= a[idx[p2]])
    idx[p] = idx[p1];
  else
    idx[p] = idx[p2];
}
int query2(int l, int r, int p, int i, int j) {
  if (i > r || j < l) return (long long)1e5 + 7 - 1;
  if (i <= l && j >= r) return idx[p];
  int mid = (l + r) / 2;
  int q1 = query2(l, mid, p * 2, i, j);
  int q2 = query2(mid + 1, r, p * 2 + 1, i, j);
  if (a[q1] <= a[q2])
    return q1;
  else
    return q2;
}
void update2(int l, int r, int p, int i, int j) {
  if (r < i || l > j) return;
  if (i <= l && j >= r) {
    a[l] = 1e8;
    return;
  }
  int mid = (l + r) / 2;
  update2(l, mid, p * 2, i, j);
  update2(mid + 1, r, p * 2 + 1, i, j);
  if (a[idx[p * 2]] <= a[idx[p * 2 + 1]])
    idx[p] = idx[p * 2];
  else
    idx[p] = idx[p * 2 + 1];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  memset(lft, -1, sizeof lft);
  a[(long long)1e5 + 7 - 1] = 1e8;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[i] = x;
    if (lft[x] == -1) lft[x] = i;
  }
  for (int i = 0; i < N; i++) sum[i] = 1;
  build(0, N - 1, 1);
  build2(0, N - 1, 1);
  int last = 0, go = N - 1;
  for (int i = 0; i < N; i++) {
    int t = query2(0, N - 1, 1, 0, N - 1);
    int ex = query2(0, N - 1, 1, last, N - 1);
    if (a[ex] != a[t]) {
      go = lft[a[t]];
    } else {
      go = ex;
    }
    if (last <= go)
      ans += query(0, N - 1, 1, last, go);
    else {
      ans += query(0, N - 1, 1, last, N - 1);
      ans += query(0, N - 1, 1, 0, go);
    }
    last = go;
    update(0, N - 1, 1, go, go);
    update2(0, N - 1, 1, go, go);
  }
  cout << ans;
  return 0;
}
