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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class q5 {
	
	
	public static void main(String[] args) throws IOException {
		
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		StringBuffer output=new StringBuffer("");
		int n=Reader.nextInt();
		TreeMap<Integer,ArrayList<Integer>> tm=new TreeMap<Integer,ArrayList<Integer>>();
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(!tm.containsKey(a))tm.put(a, new ArrayList<Integer>());
			tm.get(a).add(i);
		}
		TreeMap<Integer,Integer> lt=new TreeMap<Integer,Integer>();
		TreeMap<Integer,Integer> ut=new TreeMap<Integer,Integer>();
		TreeSet<Integer> indices=new TreeSet<Integer>();
		int lastind=-1;
		int ft=0;
		long ans=0;
		Seg tree=new Seg(n);
		while(tm.size()!=0) {
			int a=tm.firstKey();
			ArrayList<Integer> a1=tm.get(a);
			tm.remove(a);
			if(lastind==-1) {
				for(int j:a1) {
					ans+=j-lastind;
					lastind=j;
					tree.u1(0, 0, n-1, j, n-1);
					tree.u2(0, 0, n-1, 0, j);
				}
			}
			else {
				PriorityQueue<Integer> pq=new PriorityQueue<Integer>();
				for(int j:a1) {
					if(j>lastind) {
						pq.add(j-lastind);
					}
					else {
						pq.add(n-lastind+j);
					}
				}
				int ll=lastind;
				while(pq.size()!=0) {
					int aa=pq.poll();
					if(aa<n-ll) {
						int ef=aa+ll;
						ans+=ef-lastind;
						ans-=tree.query2(0, 0, n-1, lastind+1);
						if(ef!=n-1) ans+=tree.query2(0, 0, n-1, ef+1);
						tree.u2(0, 0, n-1, 0, ef);
						tree.u1(0, 0, n-1, ef, n-1);
						lastind=ef;
					}
					else {
						int ef=aa+ll-n;
						if(ef<lastind) {
							ans+=n-1-lastind+ef+1;
							if(ef!=0)
							ans-=tree.query(0, 0, n-1, ef-1);
							if(lastind!=n-1) ans-=tree.query2(0, 0, n-1, lastind+1);
						}
						else {
							ans+=ef-lastind;
							ans-=tree.query2(0, 0, n-1, lastind+1);
							if(ef!=n-1) ans+=tree.query2(0, 0, n-1, ef+1);
						}
						tree.u2(0, 0, n-1, 0, ef);
						tree.u1(0, 0, n-1, ef, n-1);
						lastind=ef;
						
					}
				}
			}
			
		}
		output.append(ans);
		out.write(output.toString());
		out.flush();
		
	}
}

/*
 64
826 142 89 337 897 891 1004 704 281 644 910 852 147 193 289 384 625 695 416 944 162 939 164 1047 359 114 499 99 713 300 268 316 256 404 852 496 373 322 716 202 689 857 936 806 556 153 137 863 1047 678 564 474 282 135 610 176 855 360 814 144 77 112 354 154
 */

class NoD{
	int a,b,c;
	
	String s;
	long ms,ls,rs,ts;
	NoD(int aa,int bb){
		a=aa;b=bb;
		s=a+" "+b;
		
	}
	NoD(int aa,int bb,int cc){
		a=aa;b=bb;c=cc;
	}
	NoD(long m,long l,long r,long t){
		ms=m;ls=l;rs=r;ts=t;
	}
	@Override
	public boolean equals(Object o) {
		if(o!=null && o.getClass()==getClass()) {
			NoD c= (NoD) o;
			return c.a==a && c.b==b;
		}
		return false;
	}
	@Override
	public int hashCode() {
		return s.hashCode();
	}
}

class Seg{
	long[] st;
	long[] st2;
	long[] arr;
	long[] lazy,lazy2;
	Seg(int n){
		st=new long[4*n];
		st2=new long[4*n];
		lazy=new long[4*n];
		lazy2=new long[4*n];
	}
	void build(int index,int low,int high) {
		if(low==high) {
			st[index]=arr[low];
		}
		else {
			int middle=(low+high)/2;
			build(2*index+1,low,middle);
			build(2*index+2,middle+1,high);
			st[index]=Math.max(st[2*index+1], st[2*index+2]);
		}
	}
	
	long query(int index,int low,int high, int pos) {
		if(lazy[index]!=0) {
			st[index]+=lazy[index];
			if(low<high) {
				lazy[2*index+1]+=lazy[index];
				lazy[2*index+2]+=lazy[index];
				
			}
			lazy[index]=0;
		}
		long tr=Long.MAX_VALUE;
		if(low==high) {
			return st[index];
		}
		else {
			int middle=(low+high)/2;
			if(pos<=middle) {
				return query(2*index+1,low,middle,pos);
			}
			else return query(2*index+2,middle+1,high,pos);
		}
		
	}
	long query2(int index,int low,int high, int pos) {
		if(lazy2[index]!=0) {
			st2[index]+=lazy2[index];
			if(low<high) {
				lazy2[2*index+1]+=lazy2[index];
				lazy2[2*index+2]+=lazy2[index];
				
			}
			lazy2[index]=0;
		}
		long tr=Long.MAX_VALUE;
		if(low==high) {
			return st2[index];
		}
		else {
			int middle=(low+high)/2;
			if(pos<=middle) {
				return query2(2*index+1,low,middle,pos);
			}
			else return query2(2*index+2,middle+1,high,pos);
		}
		
	}
	void u1(int index, int low,int high, int ql, int qh) {
		if(ql<=low && high<=qh) {
			lazy[index]+=1;
		}
		else if(!(qh<low || ql>high)) {
			int middle=(low+high)/2;
			u1(2*index+1,low,middle,ql,qh);
			u1(2*index+2,middle+1,high,ql,qh);
		}
	}
	void u2(int index, int low,int high, int ql, int qh) {
		if(ql<=low && high<=qh) {
			lazy2[index]+=1;
		}
		else if(!(qh<low || ql>high)) {
			int middle=(low+high)/2;
			u2(2*index+1,low,middle,ql,qh);
			u2(2*index+2,middle+1,high,ql,qh);
		}
	}
}
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
    	return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}