#include <bits/stdc++.h>
using namespace std;
struct ValueIndex {
  ValueIndex(int value, int index) {
    this->value = value;
    this->indexes.push_back(index);
  }
  int value;
  vector<int> indexes;
};
bool ValueIndexCmp(ValueIndex left, const ValueIndex right) {
  if (left.value == right.value) {
    return left.indexes[0] < right.indexes[0];
  }
  return left.value < right.value;
}
int main() {
  int n;
  cin >> n;
  vector<ValueIndex> numbers;
  int val = 0;
  int first = -1;
  bool allSame = true;
  for (int i = 0; i < n; i++) {
    cin >> val;
    if (first == -1) {
      first = val;
    } else if (allSame) {
      allSame = val == first;
    }
    numbers.push_back(ValueIndex(val, i));
  }
  if (allSame) {
    cout << n;
    return 0;
  }
  sort(numbers.begin(), numbers.end(), ValueIndexCmp);
  vector<ValueIndex> numbersSortedWithoutDuplicates;
  for (int i = 0; i < n; i++) {
    ValueIndex vi = numbers[i];
    while (i + 1 != n) {
      if (vi.value == numbers[i + 1].value) {
        vi.indexes.push_back(numbers[i + 1].indexes.front());
        i++;
      } else {
        break;
      }
    }
    numbersSortedWithoutDuplicates.push_back(vi);
  }
  long long result = 0;
  int lastIndex = n + 1;
  for (ValueIndex smallestValue : numbersSortedWithoutDuplicates) {
    if (smallestValue.indexes.size() == 1) {
      if (lastIndex > smallestValue.indexes.front()) {
        result += n;
      }
      lastIndex = smallestValue.indexes.front();
    } else {
      if (lastIndex > smallestValue.indexes.back()) {
        result += n;
        lastIndex = smallestValue.indexes.back();
      } else {
        int numOfElemsOnTheRight = 0;
        vector<int>::iterator low =
            upper_bound(smallestValue.indexes.begin(),
                        smallestValue.indexes.end(), lastIndex);
        numOfElemsOnTheRight = smallestValue.indexes.size() -
                               (low - smallestValue.indexes.begin());
        if (numOfElemsOnTheRight == 0) {
          lastIndex = smallestValue.indexes.back();
        } else if (numOfElemsOnTheRight == smallestValue.indexes.size()) {
          lastIndex = smallestValue.indexes.back();
        } else {
          lastIndex =
              smallestValue.indexes[low - smallestValue.indexes.begin() - 1];
          result += n - numOfElemsOnTheRight;
        }
      }
    }
    n -= smallestValue.indexes.size();
  }
  cout << result;
  return 0;
}
