#include <bits/stdc++.h>
using namespace std;
const int MM = 1e5 + 5;
int n, A[MM], idx = 1;
vector<int> pos[MM];
long long cnt = 0;
struct node {
  int l, r, val;
} seg[4 * MM];
void pushup(int rt) {
  if (seg[rt].l == seg[rt].r) return;
  seg[rt].val = seg[2 * rt].val + seg[2 * rt + 1].val;
}
void build(int rt, int l, int r) {
  seg[rt].l = l, seg[rt].r = r;
  if (l == r) {
    seg[rt].val = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * rt, l, mid), build(2 * rt + 1, mid + 1, r);
  pushup(rt);
}
void update(int rt, int i, int val) {
  if (seg[rt].l > i || seg[rt].r < i) return;
  if (seg[rt].l == i && seg[rt].r == i) {
    seg[rt].val = val;
    return;
  }
  update(2 * rt, i, val), update(2 * rt + 1, i, val);
  pushup(rt);
}
long long query(int rt, int l, int r) {
  if (seg[rt].l > r || seg[rt].r < l) return 0;
  if (l <= seg[rt].l && seg[rt].r <= r) return seg[rt].val;
  return query(2 * rt, l, r) + query(2 * rt + 1, l, r);
}
int main() {
  cin.tie(0);
  cin >> n;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  for (int i = 1; i <= n; i++) {
    int v;
    cin >> v;
    pos[v].push_back(i);
    A[i] = v;
  }
  build(1, 1, n);
  sort(A + 1, A + n + 1);
  for (int i = 1; i <= n; i++) {
    int plus = -1;
    int beh = -1, front = -1;
    for (int j = 1; j <= pos[A[i]].size(); j++) {
      plus++;
      int loc = pos[A[i]][j - 1];
      if (loc < idx)
        beh = loc;
      else
        front = loc;
      update(1, loc, 0);
    }
    if (beh == -1) {
      cnt += query(1, idx, front);
      cnt += pos[A[i]].size();
      idx = front;
    } else {
      cnt += query(1, idx, n);
      cnt += query(1, 1, beh);
      cnt += pos[A[i]].size();
      idx = beh;
    }
    i += plus;
  }
  cout << cnt << "\n";
}
