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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author AsgarJ
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        FastWriter out = new FastWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        int n;

        public void solve(int testNumber, InputReader in, FastWriter out) {
            n = in.nextInt();
            int[] a = in.readIntArray(n);

            TaskE.Entry[] entry = new TaskE.Entry[n];
            for (int i = 0; i < n; i++) {
                entry[i] = new TaskE.Entry(a[i], i + 1);
            }
            Arrays.sort(entry);
            FenwickTree.Fenwick bit = new FenwickTree.Fenwick(n + 1);
            for (int i = 0; i < n; ++i) bit.update(i + 1, 1);
            int pos = 0;
            long res = 0L;
            for (int i = 0; i < n - 1; ++i) {
                if (entry[i].v != entry[i + 1].v) {
                    res += goTo(pos, entry[i].pos, bit);
                    pos = entry[i].pos;
                    bit.update(pos, -1);
                } else {
                    int start = 0, v = entry[i].v;
                    boolean reverse = false;
                    while (i < n && entry[i].v == v) {
                        if (entry[i].pos < pos) {
                            reverse = true;
                            res += goTo(start, entry[i].pos, bit);
                            start = entry[i].pos;
                            bit.update(start, -1);
                        } else {
                            res += goTo(pos, entry[i].pos, bit);
                            pos = entry[i].pos;
                            bit.update(pos, -1);
                        }
                        i++;
                    }
                    if (i == n) --res;
                    --i;
                    if (reverse) {
                        res += bit.get(pos, n);
                        pos = start;
                    }
                }
            }
            out.println(++res);
        }

        private int goTo(int from, int to, FenwickTree.Fenwick bit) {
            if (from < to) {
                return bit.get(from, to);
            } else {
                int res = bit.get(from, n);
                res += bit.get(0, to);
                return res;
            }
        }

        static class Entry implements Comparable<TaskE.Entry> {
            int v;
            int pos;

            public Entry(int v, int pos) {
                this.v = v;
                this.pos = pos;
            }


            public int compareTo(TaskE.Entry o) {
                if (v == o.v) return Integer.compare(pos, o.pos);
                return Integer.compare(v, o.v);
            }


            public String toString() {
                return "Entry{" +
                        "v=" + v +
                        ", pos=" + pos +
                        '}';
            }

        }

    }

    static class FastWriter extends PrintWriter {
        public FastWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public FastWriter(Writer writer) {
            super(writer);
        }

        public FastWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

    }

    static class FenwickTree {
        public static class Fenwick {
            int[] bit;

            public Fenwick(int[] a) {
                bit = new int[a.length];
                for (int i = 0; i < a.length; i++)
                    update(i, a[i]);
            }

            public Fenwick(int n) {
                this.bit = new int[n];
            }

            public void update(int index, int val) {
                for (; index < this.bit.length; index |= index + 1)
                    this.bit[index] += val;
            }

            public int get(int index) {
                int ret = 0;
                for (; index >= 0; index = (index & (index + 1)) - 1)
                    ret += this.bit[index];
                return ret;
            }

            public int get(int l, int r) {
                return get(r) - get(l - 1);
            }

        }

    }

    static class InputReader extends BufferedReader {
        StringTokenizer tokenizer;

        public InputReader(InputStream inputStream) {
            super(new InputStreamReader(inputStream), 32768);
        }

        public InputReader(String filename) {
            super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(readLine());
                } catch (IOException e) {
                    throw new RuntimeException();
                }
            }
            return tokenizer.nextToken();
        }

        public Integer nextInt() {
            return Integer.valueOf(next());
        }

        public int[] readIntArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
                array[i] = nextInt();
            return array;
        }

    }
}

