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.
/**
 * Created by Aminul on 7/13/2017.
 */

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF831E_4 {
    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();
        ImplicitTreap treap = new ImplicitTreap();
        for(int i = 1; i <= n; i++){
            treap.insert(i, in.nextInt());
        }
        
        long res = 0;
        for(int i = 1; i <= n; i++){
            int idx = treap.getMinIndex();
            res += (long) idx;
            treap.doIt(idx);
        }

        pw.println(res);
        pw.close();
    }

    static class ImplicitTreap {
        Node root;

        static class Node {
            int size, value, min;
            double priority;
            Node left, right;

            public Node(int val) {
                value = val;
                size = 1;
                priority = Math.random();
                min = val;
            }
        }

        static class NodePair {
            Node left, right;

            public NodePair(Node l, Node r) {
                left = l;
                right = r;
            }
        }

        void doIt(int k){
            NodePair np1 = split(root, k);
            NodePair np2 = split(np1.left, k-1);
            root = merge(np1.right, np2.left);
        }

        int minQuery(int l ,int r){
            l--; r--;
            NodePair t1 = split(root, l);
            NodePair t2 = split(t1.right, r - l + 1);
            int res = t2.left.min;
            Node t = merge(t2.left, t2.right);
            root = merge(t1.left, t);
            return res;
        }

        public int size() {
            return size(root);
        }

        int size(Node t) {
            if (t == null) return 0;
            else return t.size;
        }

        int min(Node t){
            if (t == null) return Integer.MAX_VALUE;
            else return t.min;
        }

        void update(Node t) {
            if (t != null) t.size = size(t.left) + 1 + size(t.right);
            if (t != null) t.min = Math.min(t.value, Math.min(min(t.left), min(t.right)));
        }

        void insert(int key, int val) {
            NodePair n = split(root, key);
            root = merge(n.left, merge(new Node(val), n.right));
        }

        void remove(int k) {
            NodePair res1 = split(root, k - 1);
            NodePair res2 = split(res1.right, 1);
            root = merge(res1.left, res2.right);
        }

        int getMinIndex(){
            return getMinIndex(root, min(root));
        }

        int sizeOf(Node t){
            if(t == null) return 0;
            else return size(t.left)+1;
        }
        int getMinIndex(Node n, int min){
            if(n == null) return 0;
            if(min(n.left) == min) return  getMinIndex(n.left, min);
            else if(n.value == min) return size(n.left)+1;
            else return size(n.left) + 1 + getMinIndex(n.right, min);
        }
        NodePair split(Node n, int k) {
            NodePair res = new NodePair(null, null);
            if (n == null) return res;
            int key = size(n.left) + 1;
            if (key > k) {
                res = split(n.left, k);
                n.left = res.right;
                res.right = n;
            } else {
                res = split(n.right, k - key);
                n.right = res.left;
                res.left = n;
            }
            update(n);
            update(res.left);
            update(res.right);
            return res;
        }

        Node merge(Node t1, Node t2) {
            if (t1 == null) return t2;
            else if (t2 == null) return t1;
            Node newRoot = null;
            if (t1.priority > t2.priority) {
                t1.right = merge(t1.right, t2);
                newRoot = t1;
            } else {
                t2.left = merge(t1, t2.left);
                newRoot = t2;
            }
            update(newRoot);
            return newRoot;
        }

    }

    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());
        }

    }
}