#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
struct node {
  int x, id;
  int last, op;
  bool operator<(const node nd) const {
    if (x != nd.x) return x < nd.x;
    return op < nd.op;
  }
};
int next[100045];
node num[1000045];
int sum[10004548];
int n;
int lowbit(int x) { return x & (-x); }
void add(int x) {
  for (int i = x; i <= n; i += lowbit(i)) {
    sum[i]++;
  }
}
int getsum(int x) {
  int s = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) {
    s += sum[i];
  }
  return s;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i].x);
    num[i].id = i;
    num[i].op = 0;
  }
  if (n == 1) {
    printf("1\n");
    return 0;
  }
  sort(num + 1, num + 1 + n);
  int last = 0;
  int id = 0;
  for (int i = 1; i <= n; i++) {
    if (num[i].x != num[i - 1].x) {
      num[i].last = id;
      last = -1;
    } else
      num[i].last = num[i - 1].last;
    if (num[i].id > num[i].last)
      num[i].op = num[i].id - num[i].last;
    else
      num[i].op = num[i].id + n - num[i].last;
    if (last < num[i].op) {
      last = num[i].op;
      id = num[i].id;
    }
  }
  sort(num + 1, num + 1 + n);
  long long cnt = num[1].id;
  last = num[1].id;
  add(last);
  for (int i = 2; i <= n; i++) {
    if (num[i].id < last) {
      int tmp = getsum(num[i].id) + getsum(n) - getsum(last);
      cnt += num[i].id + n - last - tmp;
    } else {
      int tmp = getsum(num[i].id) - getsum(last);
      cnt += num[i].id - last - tmp;
    }
    last = num[i].id;
    add(last);
  }
  printf("%lld\n", cnt);
  return 0;
}
