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.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Function;

public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }
}

class Task {

    //    static int MAX = 1000001;
//    List<List<Integer>> g = new ArrayList<>();
//    int n;
//    char[] s;

    public void solve(Scanner in, PrintWriter out) {
        int n = in.nextInt();
        List<Vect> a = new ArrayList<>(n);

        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            a.add(new Vect(x, i));
        }
        a.sort(Comparator.comparingInt((Vect o) -> o.a).thenComparingInt((Vect o) -> o.b));

        //12
        //1 2 3 1 1 2 1 1 3 1 2 2
        int val = a.get(0).a;
        int start = 0;
        int mid = -1;
        int pos = -1;
        for (int i = 0; i < n; i++) {
            Vect v = a.get(i);
            if (val == v.a) {
                if (v.b > pos && mid == -1) {
                    mid = i;
                }
            }
            if (val != v.a) {
                if (mid == -1) {
                    mid = i;
                }
                swap(a, start, mid, i);
                start = i;
                pos = a.get(i - 1).b;
                mid = -1;
                val = a.get(i).a;
                if (v.b > pos && mid == -1) {
                    mid = i;
                }
            }
        }
        if (mid == -1) {
            mid = n;
        }
        swap(a, start, mid, n);

        long sum = 0;
        int t = n;
        int prev = -1;
        for (int i = 0; i < a.size(); i++) {
            int cur = a.get(i).b;
            if (cur < prev) {
                sum += t;
                t = n - i;
            }
            prev = cur;
        }
        sum += t;
        System.out.println(sum);
    }


    public void swap(List<Vect> v, int s, int mid, int f) {
        List<Vect> temp = new ArrayList<>(mid - s);
        for (int i = s; i < mid; i++) {
            temp.add(v.get(i));
        }
        for (int i = mid; i < f; i++) {
            v.set(i - temp.size(), v.get(i));
        }
        for (int i = 0; i < temp.size(); i++) {
            v.set(f - temp.size() + i, temp.get(i));
        }
    }
}

class Utils {

    public static int binarySearch(int[] a, int key) {
        int s = 0;
        int f = a.length;
        while (f > s) {
            int mid = (s + f) / 2;
            if (a[mid] > key) {
                f = mid - 1;
            } else if (a[mid] <= key) {
                s = mid + 1;
            }
        }
        return -1;
    }

    public static long gcd(long a, long b) {
        return b != 0 ? gcd(b, a % b) : a;
    }

    public static long lcm(long a, long b) {
        return a / gcd(a, b) * b;
    }

    static List<Integer> prime(int number) {
        List<Integer> a = new ArrayList<>();
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                a.add(i);
                number /= i;
                i = 1;
            }
        }
        a.add(number);
        return a;
    }
}

class Pair<T, U> {
    public T a;
    public U b;

    public Pair(T a, U b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Pair<?, ?> pair = (Pair<?, ?>) o;

        if (a != null ? !a.equals(pair.a) : pair.a != null) return false;
        return b != null ? b.equals(pair.b) : pair.b == null;
    }

    @Override
    public int hashCode() {
        int result = a != null ? a.hashCode() : 0;
        result = 31 * result + (b != null ? b.hashCode() : 0);
        return result;
    }
}


class Vect {
    public int a;
    public int b;

    public Vect(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Vect vect = (Vect) o;

        if (a != vect.a) return false;
        return b == vect.b;
    }

    @Override
    public int hashCode() {
        int result = a;
        result = 31 * result + b;
        return result;
    }
}

class Tri {
    public int a;
    public int b;
    public int c;

    public Tri(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tri tri = (Tri) o;

        if (a != tri.a) return false;
        return b == tri.b;
    }

    @Override
    public int hashCode() {
        int result = a;
        result = 31 * result + b;
        return result;
    }
}

class Triple<T, U, P> {
    public T a;
    public U b;
    public P c;

    public Triple(T a, U b, P c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Triple<?, ?, ?> triple = (Triple<?, ?, ?>) o;

        if (a != null ? !a.equals(triple.a) : triple.a != null) return false;
        return b != null ? b.equals(triple.b) : triple.b == null;
    }

    @Override
    public int hashCode() {
        int result = a != null ? a.hashCode() : 0;
        result = 31 * result + (b != null ? b.hashCode() : 0);
        return result;
    }
}


class Scanner {
    BufferedReader in;
    StringTokenizer tok;

    public Scanner(InputStream in) {
        this.in = new BufferedReader(new InputStreamReader(in));
        tok = new StringTokenizer("");
    }

    private String tryReadNextLine() {
        try {
            return in.readLine();
        } catch (Exception e) {
            throw new InputMismatchException();
        }
    }

    public String nextToken() {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(next());
        }
        return tok.nextToken();
    }

    public String next() {
        String newLine = tryReadNextLine();
        if (newLine == null)
            throw new InputMismatchException();
        return newLine;
    }

    public int nextInt() {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() {
        return Long.parseLong(nextToken());
    }
}