#include <bits/stdc++.h>
using namespace std;
const int kBlockSize = 317;
class Block {
 public:
  void Assign(const vector<int>& A, int from, int to) {
    values.clear();
    min_value = numeric_limits<int>::max();
    if (from > to) {
      return;
    }
    values.reserve(to - from + 1);
    for (int i = from; i <= to; ++i) {
      min_value = min(min_value, A[i]);
      values.push_back(A[i]);
    }
  }
  bool Split(int x, Block* left, Block* right) {
    if (min_value != x) {
      return false;
    }
    int idx = find(values.begin(), values.end(), x) - values.begin();
    left->Assign(values, 0, idx - 1);
    right->Assign(values, idx + 1, (int)values.size() - 1);
    return true;
  }
  void SplitCenter(Block* left, Block* right) {
    int idx = values.size() / 2;
    assert(idx > 0);
    left->Assign(values, 0, idx - 1);
    right->Assign(values, idx, values.size() - 1);
  }
  void MergeRight(const Block& block) {
    for (int v : block.values) {
      values.push_back(v);
      min_value = min(min_value, v);
    }
  }
  void Swap(Block* block) {
    swap(min_value, block->min_value);
    values.swap(block->values);
  }
  int GetSize() const { return values.size(); }

 private:
  int min_value;
  vector<int> values;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> A(N);
  multiset<int> M;
  for (int& a : A) {
    cin >> a;
    M.insert(a);
  }
  deque<Block> blocks;
  for (int i = 0; i < N; i += kBlockSize) {
    int j = min(N, i + kBlockSize);
    blocks.push_back(Block());
    blocks.back().Assign(A, i, j - 1);
  }
  int64_t result = 0;
  vector<Block> tmp;
  Block left, right;
  while (!M.empty()) {
    int value = *M.begin();
    M.erase(M.begin());
    tmp.clear();
    while (!blocks.empty()) {
      Block b;
      blocks.front().Swap(&b);
      blocks.pop_front();
      if (b.Split(value, &left, &right)) {
        result += left.GetSize() + 1;
        if (left.GetSize() > 0) {
          tmp.push_back(Block());
          tmp.back().Swap(&left);
        }
        if (right.GetSize() > 0) {
          blocks.push_front(Block());
          blocks.front().Swap(&right);
        }
        break;
      } else {
        result += b.GetSize();
        tmp.push_back(Block());
        tmp.back().Swap(&b);
      }
    }
    for (Block& b : tmp) {
      blocks.push_back(Block());
      blocks.back().Swap(&b);
    }
    if (blocks.size() >= 2 && blocks.back().GetSize() <= kBlockSize / 2) {
      blocks[blocks.size() - 2].MergeRight(blocks.back());
      blocks.pop_back();
      if (blocks.back().GetSize() >= 2 * kBlockSize) {
        blocks.back().SplitCenter(&left, &right);
        blocks.pop_back();
        blocks.push_back(Block());
        blocks.back().Swap(&left);
        blocks.push_back(Block());
        blocks.back().Swap(&right);
      }
    }
    if (blocks.size() >= 2 && blocks.front().GetSize() <= kBlockSize / 2) {
      blocks[0].MergeRight(blocks[1]);
      blocks.erase(blocks.begin() + 1);
      if (blocks.front().GetSize() >= 2 * kBlockSize) {
        blocks.front().SplitCenter(&left, &right);
        blocks.pop_front();
        blocks.push_front(Block());
        blocks.front().Swap(&right);
        blocks.push_front(Block());
        blocks.front().Swap(&left);
      }
    }
  }
  cout << result << '\n';
  return 0;
}
