#include <bits/stdc++.h>
using namespace std;
const signed int inf = (signed)((1u << 31) - 1);
const signed long long llf = (signed long long)((1ull << 61) - 1);
const double eps = 1e-6;
const int binary_limit = 128;
template <typename T>
inline bool readInteger(T& u) {
  char x;
  int aFlag = 1;
  while (!isdigit((x = getchar())) && x != '-' && x != -1)
    ;
  if (x == -1) {
    ungetc(x, stdin);
    return false;
  }
  if (x == '-') {
    x = getchar();
    aFlag = -1;
  }
  for (u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0')
    ;
  ungetc(x, stdin);
  u *= aFlag;
  return true;
}
typedef class IndexedTree {
 public:
  int* l;
  int size;
  IndexedTree() {}
  IndexedTree(int size) : size(size) {
    l = new int[(size + 1)];
    memset(l, 0, sizeof(int) * (size + 1));
  }
  inline void add(int idx, int val) {
    for (; idx <= size; idx += (idx & (-idx))) {
      l[idx] += val;
    }
  }
  inline int getSum(int idx) {
    int ret = 0;
    for (; idx; idx -= (idx & (-idx))) ret += l[idx];
    return ret;
  }
} IndexedTree;
typedef class SegTreeNode {
 public:
  int minid;
  int val;
  SegTreeNode *l, *r;
  SegTreeNode() {}
  inline void pushUp() {
    if (l->val <= r->val)
      minid = l->minid, val = l->val;
    else
      minid = r->minid, val = r->val;
  }
} SegTreeNode;
typedef class SegTree {
 public:
  SegTreeNode* root;
  SegTree() {}
  SegTree(int n, int* lis) { build(root, 1, n, lis); }
  void build(SegTreeNode*& node, int l, int r, int* lis) {
    node = new SegTreeNode();
    if (l == r) {
      node->minid = l, node->val = lis[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(node->l, l, mid, lis);
    build(node->r, mid + 1, r, lis);
    node->pushUp();
  }
  void update(SegTreeNode*& node, int l, int r, int idx, int val) {
    if (l == idx && r == idx) {
      node->val = val;
      return;
    }
    int mid = (l + r) >> 1;
    if (idx <= mid)
      update(node->l, l, mid, idx, val);
    else
      update(node->r, mid + 1, r, idx, val);
    node->pushUp();
  }
  void query(SegTreeNode*& node, int l, int r, int ql, int qr, int& idx,
             int& minv) {
    if (l == ql && r == qr) {
      if (node->val < minv) idx = node->minid, minv = node->val;
      return;
    }
    int mid = (l + r) >> 1;
    if (qr <= mid)
      query(node->l, l, mid, ql, qr, idx, minv);
    else if (ql > mid)
      query(node->r, mid + 1, r, ql, qr, idx, minv);
    else {
      int a = -1, b = inf;
      query(node->l, l, mid, ql, mid, a, b);
      query(node->r, mid + 1, r, mid + 1, qr, idx, minv);
      if (b <= minv) {
        minv = b, idx = a;
      }
    }
  }
} SegTree;
int n;
int* a;
IndexedTree st;
SegTree st1;
inline void init() {
  readInteger(n);
  a = new int[n + 1];
  for (int i = 1; i <= n; i++) {
    readInteger(a[i]);
  }
}
long long res = 0;
inline void solve() {
  st = IndexedTree(n);
  st1 = SegTree(n, a);
  for (int i = 1; i <= n; i++) st.add(i, 1);
  for (int fin = 0; fin < n;) {
    for (int last = 0; fin < n && last < n;) {
      int stand, idx, minv1 = inf, minv2 = inf;
      st1.query(st1.root, 1, n, 1, n, stand, minv1);
      st1.query(st1.root, 1, n, last + 1, n, idx, minv2);
      if (minv1 != minv2) {
        res += st.getSum(n) - st.getSum(last);
        break;
      }
      res += st.getSum(idx) - st.getSum(last);
      st.add(idx, -1);
      st1.update(st1.root, 1, n, idx, inf);
      fin++;
      last = idx;
    }
  }
  printf("%lld", res);
}
int main() {
  init();
  solve();
  return 0;
}
