After entering high school, Takeko, who joined the programming club, gradually became absorbed in the fun of algorithms. Now, when I'm in the second grade, I'd like to participate in Programming Koshien.

At one point, Takeko, who learned about sorting algorithms, tried to design a sorting algorithm herself. The sort algorithm created by Takeko executes the following processing when a column consisting of one or more natural numbers with no duplication between elements is given as input.

1. First, select the first element of the column.
2. When there is an element immediately before the selected element, compare the selected element with the element immediately before it. If the previous element is larger, move it immediately after the end of the column (figure). Continue this operation until the selected element is at the beginning of the column or the previous element is smaller than the selected element.
3. Finish if the selected element is at the end of the column. If not, select a new element immediately after the selected element and return to 2.

<image>



Takeko decided to count the number of operations to move the element immediately after the end of the column in order to estimate how much computational time this algorithm would take.




Write a program that takes column information as input and reports the number of operations that move an element immediately after the end of the column.



Input

The input is given in the following format.


N
a1 a2 ... aN


The number of elements N (1 ≤ N ≤ 200000) contained in the column is given in the first row. In the second row, the column elements ai (1 ≤ ai ≤ 109) are given in order from the beginning. There is no duplication in element ai.

Output

Outputs the number of operations to move an element immediately after the end of a column in one row.

Examples

Input

6
1 3 6 5 8 2


Output

10


Input

4
4 3 2 1


Output

6
#include <bits/stdc++.h>
#define FOR(i, a, b)    for (int i = a; i <= b; ++i)
#define ii              pair <int, int>
using namespace std;
const int N = 2e5 + 3;
int n, dsu[N];
long long res;
vector <ii> V;
deque <int> dq;

void init() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
}

int root(int x) { return dsu[x] <= 0 ? x : dsu[x] = root(dsu[x]); }
void merge(int u, int v) {
    if (dsu[u] > dsu[v]) swap(u, v);
    dsu[u] += dsu[v], dsu[v] = u;
}

void solve() {
    cin >> n;
    FOR(i, 1, n) { int x; cin >> x; V.push_back({ x, i }); dq.push_back(i); }

    memset(dsu, -1, sizeof dsu);
    sort(V.begin(), V.end());
    for (ii it : V) {
        while (1) {
            int id1 = root(it.second);
            int id2 = root(dq.front()); dq.pop_front();
            if (id2 == id1) break;
            merge(id1, id2);
        }
        int id1 = root(it.second);
        res += 0LL - dsu[id1] - 1; dsu[id1]++;
        if (dsu[id1] < 0) dq.push_back(id1);
    }

    cout << res << '\n';
}

int main() {
    init(); solve();
}

