Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input

The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples

Input

4
6 3 1 2


Output

7


Input

1
1000


Output

1


Input

7
3 3 3 3 3 3 3


Output

7

Note

In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.
import java.io.*;
import java.util.*;


public class CF831E_3 {
    public static int index;
    public static void main(String[] args)throws Exception {
        InputReader in = new InputReader(System.in);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        int n = in.nextInt();
        int arr[] = new int[n+1];
        segTree seg = new segTree(n);
        BIT bit = new BIT((int)1e5+50);
        for(int i = 1; i <= n; i++){
            arr[i] = in.nextInt();
            seg.update(i, arr[i]);
            bit.add(i);
        }

        long sum = 0;
        int last = 0;
        Node min = seg.query(1, n);
        for(int i = 1; i <= n; i++){
            Node node1 = seg.query(1, last-1), node2 = seg.query(last+1, n);
            if(node2.n <= node1.n) min = node2;
            else min = node1;

           // debug(last, min);
            if(min.idx > last){
                int h = 0, l = 0;
                l = bit.low(last+1);
                h = bit.low(min.idx+1);
                //debug("hl22", h, l);
                sum += (long)(h - l);
            }
            else{
                int h = 0, l = 0;
                h = bit.high(last);
                l = bit.low(min.idx+1);
                //debug("hl", h, l);
                sum += (long) ( h + l);
            }
            bit.remove(min.idx);
            last = min.idx;
            seg.update(min.idx, (int)2e9);
            //debug(sum);
        }



        pw.println(sum);

        pw.close();
    }

    static class BIT{
        public int bit[], n;

        public BIT(int nn){
            n = nn;
            bit = new int[n+1];
        }

        int low(int x){
            return sum(x-1);
        }
        int high(int x){
            return sum(n) - sum(x);
        }
        void add(int x){
            add(x, 1);
        }
        void remove(int x){
            add(x, -1);
        }
        void add(int index, int value){
            for(; index < bit.length;index = index + ( index & -index)){
                bit[index] += value;
            }
        }
        int sum(int index){
            int sum = 0;
            for(; index > 0;index = index - (index & -index)){
                sum += bit[index];
            }
            return sum;
        }
    }


    static class Node implements Comparable<Node>{
        int n, idx;
        Node(int N, int i){
            n = N; idx = i;
        }
        public int compareTo(Node N){
            if(n != N.n) return n - N.n;
            else return idx - N.idx;
        }
        public String toString(){
            return n + " "+ idx;
        }
    }

    static class segTree{
        public int n;
        Node seg[];
        public segTree(int N){
            n = N;
            seg = new Node[4*(int)1e5];
            for(int i = 0; i < seg.length; i++){
                seg[i] = new Node(0, 0);
            }
            //segTree = new int[5*n];
        }

        public void update(int pos,  int val){
            update(1, 1, n, pos, val);
        }

        public Node query(int l, int r){
            if(l < 1 || r < 1 || l > r || l > n || r > n) return new Node((int)2e9, 1000000);
            return query(1, 1, n, l, r);
        }

        public void update(int p, int s, int e, int pos, int val){
            if(s == pos && e == pos){
                seg[p].n = val;
                seg[p].idx = pos;
                return;
            }
            if(s > pos || e < pos || s > e || e < s) return;

            int m = (s+e)/2, l = 2*p, r = 2*p + 1;

            update(l, s, m, pos, val);
            update(r, m+1, e, pos, val);

            if(seg[l].n <= seg[r].n){
                seg[p].n = seg[l].n;
                seg[p].idx = seg[l].idx;
            }
            else{
                seg[p].n = seg[r].n;
                seg[p].idx = seg[r].idx;
            }
        }


        public Node query(int p, int s, int e, int a, int b){
            if(s >= a && e <= b){
                return seg[p];
            }
            if(s > b || e < a || s > e || e < s) return new Node((int)2e9, 1000000);

            int m = (s+e)/2, l = 2*p, r = 2*p + 1;

            Node q1 = query(l, s, m, a, b);
            Node q2 = query(r, m+1, e, a, b);

            if(q1.n <= q2.n){
                return q1;
            }
            else{
                return q2;
            }
        }
    }

    static void debug(Object...obj) {
        System.err.println(Arrays.deepToString(obj));
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next()throws Exception {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine()throws Exception {
            String line = null;
            tokenizer = null;
            line =  reader.readLine();
            return line;
        }

        public int nextInt()throws Exception {
            return Integer.parseInt(next());
        }

        public double nextDouble() throws Exception{
            return Double.parseDouble(next());
        }

        public long nextLong()throws Exception {
            return Long.parseLong(next());
        }

    }
}