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.util.*;
import java.io.*;

public class e {

	public static int n;
	public static ArrayList<item> list;

	public static void main(String[] args) throws Exception {

		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(stdin.readLine());
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		list = new ArrayList<item>();
		for (int i=0; i<n; i++) {
			int x = Integer.parseInt(tok.nextToken());
			list.add(new item(x, i));
		}
		Collections.sort(list);


		System.out.println(solve());
	}

	public static long solve() {

		// now have to fix the ties...
		int i = 0;

		// Go past first unique value.
		while (i < n-1 && list.get(i).value == list.get(i+1).value) i++;
		i++;

		if (i == n) return n;

		// i will be starting of a block.
		while (i < n) {

			int j = i;
			while (j<n-1 && list.get(j).value == list.get(j+1).value) j++;

			// We have a block.
			if (j > i) {

				int lastpos = list.get(i-1).index;

				int k=-1;
				for (k=i; k<=j; k++) {
					if (list.get(k).index > lastpos) {
						break;
					}
				}

				// Rotate case where we have to shift stuff around.
				if (k > i) {

					item[] tmp = new item[j-i+1];
					for (int z=i; z<=j; z++) {
						int ind = k+z-i;
						if (ind > j) ind-=(j-i+1);
						tmp[z-i] = list.get(ind);
					}
					for (int z=i; z<=j; z++)
						list.set(z, tmp[z-i]);
				}
			}

			i = j+1;

		}

		// Now I am sorted the way I want - just extract indexes.
		int[] arr = new int[n];
		for (i=0; i<n; i++) {
			arr[i] = list.get(i).index+1;
			//System.out.print(arr[i]+" ");
		}
		//System.out.println();


		long res = 0;
		bit mybit = new bit(n);

		for (i=0; i<n; i++) {

			if (i == 0) {
				res += arr[i];
			}
			else {

				if (arr[i] > arr[i-1]) {
					int range = arr[i] - arr[i-1];
					//System.out.println("range is "+range+" and bit is "+mybit.sum(arr[i-1]+1, arr[i]));
					res += (range - mybit.sum(arr[i-1]+1, arr[i]));
				}
				else {
					int range = arr[i-1] - arr[i] - 1;
					range = n- range;
					long used = mybit.atOrAbove(arr[i-1]) + mybit.sum(1,arr[i]);
					//System.out.println("here "+range+" "+used);
					res += (range-used);
				}
			}
			//System.out.println(i+" "+res);
			mybit.add(arr[i], 1L);

		}

		return res;
	}
}

class item implements Comparable<item> {

	public int value;
	public int index;

	public item(int v, int i) {
		value = v;
		index = i;
	}

	public int compareTo(item other) {
		if (this.value != other.value) return this.value - other.value;
		return this.index - other.index;
	}
}

class bit {

	public long[] cumfreq;

	// Do indexes 1 to n.
	public bit(int n) {

		int size = 1;
		while (size < n) size <<= 1;
		n = size;

		cumfreq = new long[n+1];
	}

	// Uses 1 based indexing.
	public void add(int index, long value) {
		while (index < cumfreq.length) {
			cumfreq[index] += value;
			index += Integer.lowestOneBit(index);
		}
	}

	// Returns the sum of everything upto index.
	public long sum(int index) {
		long ans = 0;
		while (index > 0) {
			ans += cumfreq[index];
			index -= (Integer.lowestOneBit(index));
		}
		return ans;
	}

	// Use 1 based indexing.
	public long sum(int low, int high) {
		return sum(high) - sum(low-1);
	}

	// Return the total number of items in the BIT.
	public long all() {
		return sum(cumfreq.length-1);
	}

	// Return the total number of items in the BIT at or above index.
	public long atOrAbove(int index) {
		long sub = 0;
		if (index > 0) sub = sum(index-1);
		return all() - sub;
	}
}