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 B implements Runnable {

    private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    private BufferedReader in;
    private PrintWriter out;
    private StringTokenizer tok = new StringTokenizer("");

    private void init() throws FileNotFoundException {
        Locale.setDefault(Locale.US);
        String fileName = "";
        if (ONLINE_JUDGE && fileName.isEmpty()) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            if (fileName.isEmpty()) {
                in = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            } else {
                in = new BufferedReader(new FileReader(fileName + ".in"));
                out = new PrintWriter(fileName + ".out");
            }
        }
    }

    String readString() {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() {
        return Integer.parseInt(readString());
    }

    long readLong() {
        return Long.parseLong(readString());
    }

    double readDouble() {
        return Double.parseDouble(readString());
    }

    int[] readIntArray(int size) {
        int[] a = new int[size];
        for (int i = 0; i < size; i++) {
            a[i] = readInt();
        }
        return a;
    }

    public static void main(String[] args) {
        //new Thread(null, new _Solution(), "", 128 * (1L << 20)).start();
        new B().run();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    @Override
    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            init();
            solve();
            out.close();
            time();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    private int aiverson(boolean good) {
        return good ? 1 : 0;
    }

    int[][] buildGraph(int n, int[] u, int[] v) {
        int[][] graph = new int[n][];
        int[] q = new int[n];
        for (int i = 0; i < u.length; i++) {
            q[u[i]]++;
            q[v[i]]++;
        }
        for (int i = 0; i < n; i++) {
            graph[i] = new int[q[i]];
        }
        for (int i = 0; i < u.length; i++) {
            graph[u[i]][--q[u[i]]] = v[i];
            graph[v[i]][--q[v[i]]] = u[i];
        }
        return graph;
    }

    private void solve() {
        int n = readInt();
        int[] a = readIntArray(n);
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            b[i] = a[i];
        }
        //Arrays.sort(b);
        TreeSet<Integer>[] arr = new TreeSet[1000 * 100 + 2];
        for (int i = 0; i < n; i++) {
            if (arr[b[i]] == null) {
                arr[b[i]] = new TreeSet<>();
            }
            arr[b[i]].add(i);
        }

        int lastPos = -1;
        int[] d = new int[n];
        int time = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].first() > lastPos) {
                    for (int pos : arr[i]) {
                        d[lastPos = pos] = time++;
                    }
                } else {
                    SortedSet<Integer> firstTreeSet = arr[i].subSet(lastPos + 1, n);
                    SortedSet<Integer> secondTreeSet = arr[i].subSet(0, lastPos);
                    for (int pos : firstTreeSet) {
                        d[lastPos = pos] = time++;
                    }
                    for (int pos : secondTreeSet) {
                        d[lastPos = pos] = time++;
                    }
                }
            }
        }

        int[] c = new int[n];
        for (int i = 0; i < n; i++) {
           c[d[i]] = i;
        }

        t = new int[n];
        for (int i = 0; i < n; i++) {
            inc(i, 1);
        }

        long answer = 0;
        int cur = 0;
        for (int i = 0; i < n; i++) {
            if (cur > c[i]) {
                answer += sum(cur, n - 1);
                answer += sum(c[i]);
            } else {
                answer += sum(cur, c[i]);
            }
            inc(c[i], -1);
            cur = c[i];
        }

        out.println(answer);
    }

    int[] t;

    int sum(int r) {
        int result = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            result += t[r];
        return result;
    }

    void inc(int i, int delta) {
        for (; i < t.length; i = (i | (i + 1)))
            t[i] += delta;
    }

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