#include <bits/stdc++.h>
using namespace std;
struct Node {
  int value, priority;
  int min, size;
  Node *left_child = nullptr, *right_child = nullptr;
  Node(const int value) : value(value), priority(rand()), min(value), size(1) {}
};
int min(Node* const root) {
  return root ? root->min : numeric_limits<int>::max();
}
int size(Node* const root) { return root ? root->size : 0; }
void recalculate(Node* root) {
  root->min = min({min(root->left_child), root->value, min(root->right_child)});
  root->size = size(root->left_child) + 1 + size(root->right_child);
}
pair<Node*, Node*> split(Node* const root, int goal) {
  if (!root) return {nullptr, nullptr};
  Node *left, *right;
  if (size(root->left_child) >= goal) {
    right = root;
    tie(left, right->left_child) = split(root->left_child, goal);
    recalculate(right);
  } else {
    goal -= size(root->left_child) + 1;
    left = root;
    tie(left->right_child, right) = split(root->right_child, goal);
    recalculate(left);
  }
  return {left, right};
}
Node* merge(Node* const left, Node* const right) {
  if (!left || !right) return left ? left : right;
  if (left->priority > right->priority) {
    left->right_child = merge(left->right_child, right);
    recalculate(left);
    return left;
  } else {
    right->left_child = merge(left, right->left_child);
    recalculate(right);
    return right;
  }
}
int min_element(Node* const root) {
  if (min(root->left_child) == min(root))
    return min_element(root->left_child);
  else if (root->value == min(root))
    return size(root->left_child);
  else
    return size(root->left_child) + 1 + min_element(root->right_child);
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  Node* root = nullptr;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    root = merge(root, new Node(x));
  }
  long long result = 0;
  for (int i = 0; i < n; i++) {
    const int j = min_element(root);
    Node *t1, *t2;
    tie(t1, t2) = split(root, j);
    root = merge(t2, t1);
    result += j;
    tie(ignore, root) = split(root, 1);
    result++;
  }
  cout << result << endl;
}
