#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a>b?b:a)
#define INF 0x3f3f3f3f
#define sz size()
#define pb push_back
#define ph push
#define mem(x, y) memset(x, y, sizeof(x));
#define mem0(x) mem(x,0)
#define mem1(x) mem(x,-1)
#define memINF(x) mem(x,INF)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<(b);++i)
using namespace std;
typedef __int64 ll;

int const Max_N = 100000 + 50;

vector<int>v[Max_N];
int a[Max_N] = {0};
int c[Max_N] = {0};
int n;
void update(int i,int x)
{
	while(i<=n)
	{
		c[i] += x;
		i += lowbit(i);
	}
}

int sum(int i)
{
	int ans = 0;
	while(i>0)
	{
		ans += c[i];
		i -= lowbit(i);
	}
	return ans;
}

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

int main()
{
 	cin >> n;
 	rep(i,1,n+1)
 	{
 		update(i,1);
 		cin >> a[i];
 		v[a[i]].pb(i);
	}

	sort(a+1,a+1+n);
	ll ans = 0;
	int pos = 0;
	rep(i,1,n+1)
	{
		int k = lower_bound(all(v[a[i]]),pos) - v[a[i]].begin();
		//cout << k << endl;
		if(k>=v[a[i]].sz)
		{
			int t = v[a[i]][0];
			ans += qry(pos,n) + qry(1,t);
			update(t,-1);
			v[a[i]].erase(v[a[i]].begin());
			pos = t;
		}
		else
		{
			int t = v[a[i]][k];
			ans += qry(pos,t);
			update(t,-1);
			v[a[i]].erase(v[a[i]].begin()+k);
			pos = t;
		}
	}

	cout << ans  << endl;
	return 0;
}
	//freopen("in.txt","r",stdin);
//std::ios::sync_with_stdio(false);


#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a>b?b:a)
#define INF 0x3f3f3f3f
#define sz size()
#define pb push_back
#define ph push
#define mem(x, y) memset(x, y, sizeof(x));
#define mem0(x) mem(x,0)
#define mem1(x) mem(x,-1)
#define memINF(x) mem(x,INF)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<(b);++i)
using namespace std;
typedef __int64 ll;

int const Max_N = 100000 + 50;

vector<int>v[Max_N];
int a[Max_N] = {0};
int c[Max_N] = {0};
int n;
void update(int i,int x)
{
	while(i<=n)
	{
		c[i] += x;
		i += lowbit(i);
	}
}

int sum(int i)
{
	int ans = 0;
	while(i>0)
	{
		ans += c[i];
		i -= lowbit(i);
	}
	return ans;
}

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

int main()
{
 	cin >> n;
 	rep(i,1,n+1)
 	{
 		update(i,1);
 		cin >> a[i];
 		v[a[i]].pb(i);
	}

	sort(a+1,a+1+n);
	ll ans = 0;
	int pos = 0;
	rep(i,1,n+1)
	{
		int k = lower_bound(all(v[a[i]]),pos) - v[a[i]].begin();
		//cout << k << endl;
		if(k>=v[a[i]].sz)
		{
			int t = v[a[i]][0];
			ans += qry(pos,n) + qry(1,t);
			update(t,-1);
			v[a[i]].erase(v[a[i]].begin());
			pos = t;
		}
		else
		{
			int t = v[a[i]][k];
			ans += qry(pos,t);
			update(t,-1);
			v[a[i]].erase(v[a[i]].begin()+k);
			pos = t;
		}
	}

	cout << ans  << endl;
	return 0;
}
	//freopen("in.txt","r",stdin);aetefaaefaefaef
//std::ios::sync_with_stdio(false);


#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a>b?b:a)
#define INF 0x3f3f3f3f
#define sz size()
#define pb push_back
#define ph push
#define mem(x, y) memset(x, y, sizeof(x));
#define mem0(x) mem(x,0)
#define mem1(x) mem(x,-1)
#define memINF(x) mem(x,INF)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<(b);++i)
using namespace std;
typedef __int64 ll;

int const Max_N = 100000 + 50;

vector<int>v[Max_N];
int a[Max_N] = {0};
int c[Max_N] = {0};
int n;
void update(int i,int x)
{
	while(i<=n)
	{
		c[i] += x;
		i += lowbit(i);
	}
}

int sum(int i)
{
	int ans = 0;
	while(i>0)
	{
		ans += c[i];
		i -= lowbit(i);
	}
	return ans;
}

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

int main()
{
 	cin >> n;
 	rep(i,1,n+1)
 	{
 		update(i,1);
 		cin >> a[i];
 		v[a[i]].pb(i);
	}

	sort(a+1,a+1+n);
	ll ans = 0;
	int pos = 0;
	rep(i,1,n+1)
	{
		if(a[i] == a[i-1]) continue;

		auto it = lower_bound(all(v[a[i]]),pos);
		int k = it - v[a[i]].begin();
		//cout << k << endl;
		int t;
		if(k<v[a[i]].sz)
		{
			t = v[a[i]][v[a[i]].sz-1];
			ans += qry(pos,t);

		//	cout << "pos = " << pos << " t = " << t << " sum = " << qry(pos,t) <<  endl;
			pos = t;
		}
		rep(j,k,v[a[i]].sz)
		{
			t = v[a[i]][j];
			update(t,-1);
		}
		if(k>0)
		{
			t = v[a[i]][k-1];
			ans += qry(1,t) + qry(pos,n);
			pos = t;
			rep(j,0,k)
			{
				t = v[a[i]][j];
				update(t,-1);
			}

		}
		//cout << ans << endl;
	}

	cout << ans  << endl;
	return 0;
}


#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "algorithm"
#include "vector"
#include "queue"
#include "stack"
#include "set"
#include "map"

using namespace std;

const int maxn=100010;
int n;
int tree[maxn];
inline int lb(int x) {
    return x&(-x);
}

void insert(int s,int x) {
    while(s<maxn) {
        tree[s]+=x;
        s+=lb(s);
    }
}

int ask(int s) {
    int re=0;
    while (s) {
        re+=tree[s];
        s-=lb(s);
    }
    return re;
}

pair<int,int> p[maxn];
bool cmp(pair<int,int> a,pair<int,int> b) {
    return a.first==b.first?a.second<b.second:a.first<b.first;
}

int ls=0;
bool cmp2(pair<int,int> a,pair<int,int> b) {
    int ar=((a.second-ls+n)%n);
    int br=((b.second-ls+n)%n);
    if (ar==0)
        ar=n;
    if (br==0)
        br=n;
    return ar<br;
}

int dist(int a,int b) {
    if (b>=a) {
        return ask(b)-ask(a);
    }
    else {
        return dist(a,n)+dist(0,b);
    }
}

int main() {
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        scanf("%d",&p[i].first);
        p[i].second=i+1;
        insert(i+1,1);
    }
    sort(p,p+n,cmp);
    for (int i=0; i<(n-1); i++) {
        int l=i;
        int r=i;
        if (p[i].first==p[i+1].first) {
            l=i;
            r=i+1;
            while ((r+1)<n&&p[r+1].first==p[l].first) {
                r++;
            }
            sort(p+l,p+r+1,cmp2);
        }
        i=r;
        ls=p[r].second;
    }

    ls=0;
    long long int ans=0;
    for (int i=0; i<n; i++) {
        ans+=dist(ls,p[i].second);
        insert(p[i].second,-1);
        ls=p[i].second;
    }
    printf("%I64d\n",ans);

    return 0;
}


#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "algorithm"
#include "vector"
#include "queue"
#include "stack"
#include "set"
#include "map"

using namespace std;

const int maxn=100010;
int n;
int tree[maxn];
inline int lb(int x) {
    return x&(-x);
}

void insert(int s,int x) {
    while(s<maxn) {
        tree[s]+=x;
        s+=lb(s);
    }
}

int ask(int s) {
    int re=0;
    while (s) {
        re+=tree[s];
        s-=lb(s);
    }
    return re;
}

pair<int,int> p[maxn];
bool cmp(pair<int,int> a,pair<int,int> b) {
    return a.first==b.first?a.second<b.second:a.first<b.first;
}

int ls=0;
bool cmp2(pair<int,int> a,pair<int,int> b) {
    int ar,br;
    if (a.second>ls)
        ar=a.second-ls;
    else
        ar=(n-ls)+a.second;
    if (b.second>ls)
        br=b.second-ls;
    else
        br=(n-ls)+b.second;
    return ar<br;
}

int dist(int a,int b) {
    if (b>=a) {
        return ask(b)-ask(a);
    }
    else {
        return dist(a,n)+dist(0,b);
    }
}

int main() {
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        scanf("%d",&p[i].first);
        p[i].second=i+1;
        insert(i+1,1);
    }
    sort(p,p+n,cmp);
    for (int i=0; i<(n-1); i++) {
        int l=i;
        int r=i;
        if (p[i].first==p[i+1].first) {
            l=i;
            r=i+1;
            while ((r+1)<n&&p[r+1].first==p[l].first) {
                r++;
            }
            sort(p+l,p+r+1,cmp2);
        }
        i=r;
        ls=p[r].second;
    }

    ls=0;
    int ans=0;
    for (int i=0; i<n; i++) {
        ans+=dist(ls,p[i].second);
        insert(p[i].second,-1);
        ls=p[i].second;
    }
    printf("%d\n",ans);

    return 0;
}


#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "algorithm"
#include "vector"
#include "queue"
#include "stack"
#include "set"
#include "map"

using namespace std;

const int maxn=100010;
int n;
int tree[maxn];
inline int lb(int x) {
    return x&(-x);
}

void insert(int s,int x) {
    while(s<maxn) {
        tree[s]+=x;
        s+=lb(s);
    }
}

int ask(int s) {
    int re=0;
    while (s) {
        re+=tree[s];
        s-=lb(s);
    }
    return re;
}

pair<int,int> p[maxn];
bool cmp(pair<int,int> a,pair<int,int> b) {
    return a.first==b.first?a.second<b.second:a.first<b.first;
}

int ls=0;
bool cmp2(pair<int,int> a,pair<int,int> b) {
    int ar=((a.second-ls+n)%n);
    int br=((b.second-ls+n)%n);
    if (ar==0)
        ar=n;
    if (br==0)
        br=n;
    return ar<br;
}

int dist(int a,int b) {
    if (b>=a) {
        return ask(b)-ask(a);
    }
    else {
        return dist(a,n)+dist(0,b);
    }
}

int main() {
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        scanf("%d",&p[i].first);
        p[i].second=i+1;
        insert(i+1,1);
    }
    sort(p,p+n,cmp);
    for (int i=0; i<(n-1); i++) {
        int l=i;
        int r=i;
        if (p[i].first==p[i+1].first) {
            l=i;
            r=i+1;
            while ((r+1)<n&&p[r+1].first==p[l].first) {
                r++;
            }
            sort(p+l,p+r+1,cmp2);
        }
        i=r;
        ls=p[r].second;
    }

    ls=0;
    int ans=0;
    for (int i=0; i<n; i++) {
        ans+=dist(ls,p[i].second);
        insert(p[i].second,-1);
        ls=p[i].second;
    }
    printf("%d\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=j;i<=n;i++)
typedef long long ll;
const int maxn = 101000;
const int inf=1e9;
int n,m,x,y,g,ans,k;
struct noob
{
    int x,y;
}a[maxn];
bool cmp(noob x,noob y)
{
    return x.x<y.x||(x.x==y.x&&x.y<y.y);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    rep(i,1,n)
    {
        cin>>a[i].x;
        a[i].y=i;
    }
    sort(a+1,a+n+1,cmp);
    long long ans=n;
    rep(i,1,n-1)
        if(a[i].y>a[i+1].y&&a[i].x!=a[i+1].x)
        {
            int j=i+1;
            while(a[j].x==a[i+1].x)j++;
            j--;
            if(a[j].y>a[i].y)
            {
                noob tmp=a[j];
                for(int t=j;t>i;t--) a[t]=a[t-1];
                a[i+1]=tmp;
            }
        }
    rep(i,1,n-1)
        if(a[i].y>a[i+1].y) ans+=n-i;
    printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=j;i<=n;i++)
typedef long long ll;
const int maxn = 101000;
const int inf=1e9;
int n,m,x,y,g,ans,k;
struct noob
{
    int x,y;
}a[maxn],b[maxn];
bool cmp(noob x,noob y)
{
    return x.x<y.x||(x.x==y.x&&x.y<y.y);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    rep(i,1,n)
    {
        cin>>a[i].x;
        a[i].y=i;
    }
    sort(a+1,a+n+1,cmp);
    //rep(i,1,n) printf("%d\n",a[i].y);
    long long ans=n;
    rep(i,1,n-1)
        if(a[i].y>a[i+1].y&&a[i].x!=a[i+1].x)
        {
            int j=i+1;
            while(a[j].x==a[i+1].x&&a[j].y<a[i].y)j++;
            if(a[j].y>a[i].y&&a[j].x==a[i+1].x)
            {
                int k=j;
                while(a[j].x==a[k+1].x) k++;
                rep(t,j,k) b[t]=a[t];
                for(int t=j-1;t>i;t--) a[t-j+k+1]=a[t];
                rep(t,i+1,i-j+k+1) a[t]=b[t-i-1+j];
            }
        }
    //rep(i,1,n) printf("%d\n",a[i].y);
    rep(i,1,n-1)
        if(a[i].y>a[i+1].y) ans+=n-i;
    printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=j;i<=n;i++)
typedef long long ll;
const int maxn = 101000;
const int inf=1e9;
int n,m,x,y,g,ans,k;
struct noob
{
    int x,y;
}a[maxn];
bool cmp(noob x,noob y)
{
    return x.x<y.x||(x.x==y.x&&x.y<y.y);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    rep(i,1,n)
    {
        cin>>a[i].x;
        a[i].y=i;
    }
    sort(a+1,a+n+1,cmp);
    long long ans=n;
    rep(i,1,n-1)
        if(a[i].y>a[i+1].y) ans+=(n-i);
    printf("%I64d\n",ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<math.h>
using namespace std;

typedef long long int lli;
#define fori(x,n) for(int x = 0; x < n; ++x)
#define pb push_back
#define mp make_pair

const int MAX=1e5+5;
int BIT[MAX],n,a[MAX];
vector<int>vec[MAX];

void add(int idx,int val)
{
    for(int i=idx;i<MAX;i+=(i&(-i)))
    {
        BIT[i]+=val;
    }
}

int get(int idx)
{
    int res=0;
    for(int i=idx;i>0;i-=(i&(-i)))
    {
        res+=BIT[i];
    }
    return res;
}

int main()
{
    scanf("%d",&n);
    fori(i,n)
    {
        scanf("%d",&a[i+1]);
        vec[a[i+1]].pb(i+1);
    }

    int ans=0;

    for(int i=1;i<=n;i++)
        add(i,1);

    //for(int i=1;i<=n;i++)
        //cout << BIT[i] << " ";

    int cur=0,prev=0,till;
    for(int i=1;i<MAX;i++)
    {
        till=vec[i].size();
        //if(till)
            //cout << till << "  ";
        for(int j=0;j<vec[i].size();j++)
        {
            //cout <<" allo ";
            if(vec[i][j]>cur)
            {
                till=min(till,j);
                ans+=(get(vec[i][j])-get(cur));
                add(vec[i][j],-1);
                cur=vec[i][j];
            }
        }

        for(int j=0;j<till;j++)
        {
            //cout <<" hello ";
            if(cur<vec[i][j])
                ans+=(get(vec[i][j])-get(cur));
            else
                ans+=(get(vec[i][j]) + (get(n)-get(cur-1)));
            add(vec[i][j],-1);
            cur=vec[i][j];
        }

        //cout << endl << ans << endl;

    }

    cout << ans << endl;

    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<math.h>
using namespace std;

typedef long long int lli;
#define fori(x,n) for(int x = 0; x < n; ++x)
#define pb push_back
#define mp make_pair

const int MAX=1e5+5;
int BIT[MAX],n,a[MAX];
vector<int>vec[MAX];

void add(int idx,int val)
{
    for(int i=idx;i<MAX;i+=(i&(-i)))
    {
        BIT[i]+=val;
    }
}

lli get(int idx)
{
    lli res=0;
    for(int i=idx;i>0;i-=(i&(-i)))
    {
        res+=BIT[i];
    }
    return res;
}

int main()
{
    scanf("%d",&n);
    fori(i,n)
    {
        scanf("%d",&a[i+1]);
        vec[a[i+1]].pb(i+1);
    }

    lli ans=0;

    for(int i=1;i<=n;i++)
        add(i,1);

    //for(int i=1;i<=n;i++)
        //cout << BIT[i] << " ";

    int cur=0,prev=0,till;
    for(int i=1;i<MAX;i++)
    {
        till=vec[i].size();
        //if(till)
            //cout << till << "  ";
        for(int j=0;j<vec[i].size();j++)
        {
            //cout <<" allo ";
            if(vec[i][j]>cur)
            {
                till=min(till,j);
                ans+=(get(vec[i][j])-get(cur));
                add(vec[i][j],-1);
                cur=vec[i][j];
            }
        }

        for(int j=0;j<till;j++)
        {
            //cout <<" hello ";
            if(cur<vec[i][j])
                ans+=(get(vec[i][j])-get(cur));
            else
                ans+=(get(vec[i][j]) + (get(n)-get(cur-1)));
            add(vec[i][j],-1);
            cur=vec[i][j];
        }

        //cout << endl << ans << endl;

    }

    cout << ans << endl;

    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<math.h>
using namespace std;

typedef long long int lli;
#define fori(x,n) for(int x = 0; x < n; ++x)
#define pb push_back
#define mp make_pair

const int MAX=1e5+5;
int BIT[MAX],n,a[MAX];
vector<int>vec[MAX];

void add(int idx,int val)
{
    for(int i=idx;i<=n;i+=(i&(-i)))
    {
        BIT[i]+=val;
    }
}

int get(int idx)
{
    int res=0;
    for(int i=idx;i>0;i-=(i&(-i)))
    {
        res+=BIT[i];
    }
    return res;
}

int main()
{
    scanf("%d",&n);
    fori(i,n)
    {
        scanf("%d",&a[i+1]);
        vec[a[i+1]].pb(i+1);
    }
    int ans=0;

    for(int i=1;i<=n;i++)
        add(i,1);

    //for(int i=1;i<=n;i++)
        //cout << BIT[i] << " ";

    int cur=0,prev=0,till;
    for(int i=1;i<MAX;i++)
    {
        till=vec[i].size();
        for(int j=0;j<vec[i].size();j++)
        {

            if(vec[i][j]>cur)
            {
                till=min(till,j);
                ans+=(get(vec[i][j])-get(cur));
                add(vec[i][j],-1);
                cur=vec[i][j];
            }
        }

        for(int j=0;j<till;j++)
        {
            ans+=(get(vec[i][j]) + (get(n)-get(cur-1)));
            add(vec[i][j],-1);
            cur=vec[i][j];
        }

    }

    cout << ans << endl;

    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<math.h>
using namespace std;

typedef long long int lli;
#define fori(x,n) for(int x = 0; x < n; ++x)
#define pb push_back
#define mp make_pair

const int MAX=1e5+5;
int BIT[MAX],n,a[MAX];
vector<int>vec[MAX];

void add(int idx,int val)
{
    for(int i=idx;i<MAX;i+=(i&(-i)))
    {
        BIT[i]+=val;
    }
}

lli get(int idx)
{
    lli res=0;
    for(int i=idx;i>0;i-=(i&(-i)))
    {
        res+=BIT[i];
    }
    return res;
}

int main()
{
    scanf("%d",&n);
    fori(i,n)
    {
        scanf("%d",&a[i+1]);
        vec[a[i+1]].pb(i+1);
    }

    lli ans=0;

    for(int i=1;i<=n;i++)
        add(i,1);

    //for(int i=1;i<=n;i++)
        //cout << BIT[i] << " ";

    int cur=0,prev=0,till;
    for(int i=1;i<MAX;i++)
    {
        till=vec[i].size();
        //if(till)
            //cout << till << "  ";
        for(int j=0;j<vec[i].size();j++)
        {
            //cout <<" allo ";
            if(vec[i][j]>cur)
            {
                till=min(till,j);
                ans+=(get(vec[i][j])-get(cur));
                add(vec[i][j],-1);
                cur=vec[i][j];
            }
        }

        for(int j=0;j<till;j++)
        {
            //cout <<" hello ";
            if(cur<vec[i][j])
                ans+=(get(vec[i][j])-get(cur));
            else
                ans+=(get(vec[i][j]) + (get(n)-get(cur)));
            add(vec[i][j],-1);
            cur=vec[i][j];
        }

        //cout << endl << ans << endl;

    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e5+5;
struct node{
    LL x;
    int id;
}a[maxn];
bool cmp(node a, node b)
{
    return a.x<b.x;
}
long long val[maxn];
int lowbit(int x)
{
    return x&-x;
}
void add(int x, int y)
{
    while(x<maxn)
    {
        val[x]+=y;
        x+=lowbit(x);
    }
    return;
}

long long sum(int x)
{
    long long res=0;
    while(x>0)
    {
        res+=val[x];
        x-=lowbit(x);
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    int i, j;
    for(i=1; i<=n; i++)
    {
        scanf("%lld", &a[i].x);
        a[i].id=i;
    }
    sort(a+1, a+1+n, cmp);
    int last, id;
    j=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        add(id, 1);
    }
    int ans=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        if(i==1)
        {
            last=id;
            j=1;
            add(id, -1);
        }
        else
        {
            if(a[i].id<a[i-1].id)
            {
                ans+=j+sum(last);
//                printf("%d %lld\n", id, a[i-1].x);
                j=0;
            }
            j++;
            last=id;
            add(id, -1);
        }
    }
//    cout<<ans<<endl;
    cout<<ans+j<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e5+5;
struct node{
    LL x;
    int id;
}a[maxn];
bool cmp(node a, node b)
{
    return a.x<b.x;
}
long long val[maxn];
int lowbit(int x)
{
    return x&-x;
}
void add(int x, int y)
{
    while(x<maxn)
    {
        val[x]+=y;
        x+=lowbit(x);
    }
    return;
}

long long sum(int x)
{
    long long res=0;
    while(x>0)
    {
        res+=val[x];
        x-=lowbit(x);
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    int i;
    LL j;
    for(i=1; i<=n; i++)
    {
        scanf("%lld", &a[i].x);
        a[i].id=i;
    }
    sort(a+1, a+1+n, cmp);
    int last, id;
    j=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        add(id, 1);
    }
    LL ans=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        if(i==1)
        {
            last=id;
            j=1;
            add(id, -1);
        }
        else
        {
            if(a[i].id<a[i-1].id)
            {
                ans+=j+sum(last);
//                printf("%d %lld\n", id, a[i-1].x);
                j=0;
            }
            j++;
            last=id;
            add(id, -1);
        }
    }
//    cout<<ans<<endl;
    cout<<ans+j<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e5+5;
struct node{
    LL x;
    int id;
}a[maxn];
bool cmp(node a, node b)
{
    return a.x<b.x;
}
long long val[maxn];
int lowbit(int x)
{
    return x&-x;
}
void add(int x, int y)
{
    while(x<maxn)
    {
        val[x]+=y;
        x+=lowbit(x);
    }
    return;
}

long long sum(int x)
{
    long long res=0;
    while(x>0)
    {
        res+=val[x];
        x-=lowbit(x);
    }
    return res;
}
set<int>s[maxn];
set<int>::iterator it;
int main()
{
    int n;
    cin>>n;
    int i;
    LL j;
    for(i=1; i<=n; i++)
    {
        scanf("%lld", &a[i].x);
        a[i].id=i;
    }
    sort(a+1, a+1+n, cmp);
    j=0;
    s[j].insert(a[1].id);
    for(i=2; i<=n; i++)
    {
        if(a[i].x==a[i-1].x)s[j].insert(a[i].id);
        else
        {
            j++;
            s[j].insert(a[i].id);
        }
    }
    int m=j;
    int last, id;
    j=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        add(id, 1);
    }
    LL ans=0;
    last=0, j=0;
    for(i=0; i<=m; i++)
    {
        auto mid=s[i].upper_bound(last);
        for(it=mid; it!=s[i].end(); it++)
        {
    //        printf("%d\n", *it);
            j++;
            add(*it, -1);
            last=*it;
        }
        for(it=s[i].begin(); it!=mid; it++)
        {
    //        printf("%d\n", *it);
            if(it==s[i].begin())
            {
                ans+=j+sum(n);
    //            printf("%lld %d %lld\n", j, last, ans);
                j=0;
            }
            add(*it, -1);
            j++;
            last=*it;
        }
    }
//    cout<<ans<<endl;
    cout<<ans+j<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e5+5;
struct node{
    LL x;
    int id;
}a[maxn];
bool cmp(node a, node b)
{
    return a.x<b.x;
}
long long val[maxn];
int lowbit(int x)
{
    return x&-x;
}
void add(int x, int y)
{
    while(x<maxn)
    {
        val[x]+=y;
        x+=lowbit(x);
    }
    return;
}

long long sum(int x)
{
    long long res=0;
    while(x>0)
    {
        res+=val[x];
        x-=lowbit(x);
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    int i;
    LL j;
    for(i=1; i<=n; i++)
    {
        scanf("%lld", &a[i].x);
        a[i].id=i;
    }
    sort(a+1, a+1+n, cmp);
    int last, id;
    j=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        add(id, 1);
    }
    LL ans=0;
    for(i=1; i<=n; i++)
    {
        id=a[i].id;
        if(i==1)
        {
            last=id;
            j=1;
            add(id, -1);
        }
        else
        {
            if(a[i].id<a[i-1].id)
            {
                ans+=j+sum(n);
//                printf("%d %lld\n", id, a[i-1].x);
                j=0;
            }
            j++;
            last=id;
            add(id, -1);
        }
    }
//    cout<<ans<<endl;
    cout<<ans+j<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
typedef long long ll;
using namespace std;

int main() {
	ios::sync_with_stdio(0);
	ll n, ans, loc = 0;
	cin >> n, ans = n;
	vector<int> a(n);
	vector<set<int>> s(100001);
	for (int i = 0; i < n; i++) cin >> a[i], s[a[i]].insert(i);
	sort(a.begin(), a.end());
	for (int i = 0; i < n; i++) {
		set<int>::iterator it = s[a[i]].lower_bound(loc);
		if (it == s[a[i]].end())
			ans += n - i, loc = 0, it = s[a[i]].lower_bound(loc);
		loc = *it, s[a[i]].erase(it);
	}
	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(pair<int, int> &a, pair<int, int> &b) {
	if (a.first != b.first) return a.first < b.first;
	return a.second < b.second;
}

void show(vector<pair<int, int>> &a) {
	for (auto i : a) cout << i.first << " " << i.second << endl; cout << endl;
}

int main() {
	int n;
	cin >> n;
	vector<pair<int, int>> a(n);
	for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i + 1;
	sort(a.begin(), a.end(), cmp);
	for (int i = 1; i < n; i++) {
		//show(a);
		if (a[i].second < a[i - 1].second)
			a[i].second += a[i - 1].second;
	}
	//show(a);
	cout << a[n - 1].second << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;

bool cmp(pair<ll, ll> &a, pair<ll, ll> &b) {
	if (a.first != b.first) return a.first < b.first;
	return a.second < b.second;
}

void show(vector<pair<ll, ll>> &a) {
	for (auto i : a) cout << i.first << " " << i.second << endl; cout << endl;
}

int main() {
	ll n;
	cin >> n;
	vector<pair<ll, ll>> a(n);
	for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i + 1;
	sort(a.begin(), a.end(), cmp);
	for (int i = 1; i < n; i++) {
		//show(a);
		if (a[i].second < a[i - 1].second)
			a[i].second += a[i - 1].second;
	}
	//show(a);
	cout << a[n - 1].second << endl;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
#define ___ freopen("c:\\code\\in.txt","r",stdin);
const int N=1e6+7;
int _t;
//键值可重，去重需要加num记录这个数的个数
//有区间翻转时建树的数组下标全部加1,建树要建n+2个节点
struct Splay_tree
{
    int root,q[N];bool rev[N];
    int key[N],sz[N],f[N],ch[N][2],add[N];
    void init(){root=_t=0;}//初始化为一棵空树
    void rev1(int x){if(x)swap(ch[x][0],ch[x][1]),rev[x]^=1;}
    void add1(int x,int p){if(x)key[x]+=p,add[x]+=p;}
    inline void nw(int &x,int val,int fa)
    {
        x=++_t,key[x]=val,f[x]=fa,sz[x]=1;
        ch[x][0]=ch[x][1]=0;
        add[x]=rev[x]=0;
    }
    inline void pd(int x)
    {
        if(add[x])add1(ch[x][0],add[x]),add1(ch[x][1],add[x]),add[x]=0;
        if(rev[x])rev1(ch[x][0]),rev1(ch[x][1]),rev[x]=0;
    }
    inline void up(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
    void rotate(int x){
        int y=f[x],w=ch[y][1]==x;
        ch[y][w]=ch[x][w^1];
        if(ch[x][w^1])f[ch[x][w^1]]=y;
        if(f[y]){
            int z=f[y];
            if(ch[z][0]==y)ch[z][0]=x;
            if(ch[z][1]==y)ch[z][1]=x;
        }
        f[x]=f[y],ch[x][w^1]=y,f[y]=x,up(y);
    }

    void splay(int x,int w){
        int s=1,i=x,y;q[1]=x;
        while(f[i])q[++s]=i=f[i];
        while(s)pd(q[s--]);
        while(f[x]!=w){
            y=f[x];
            if(f[y]!=w){if((ch[f[y]][0]==y)^(ch[y][0]==x))rotate(x);else rotate(y);}
            rotate(x);
        }
        if(!w)root=x;
        up(x);
    }
    void build(int &x,int l,int r,int fa=0)//按照数组下标建树
    {
        if(l>r)return;
        int mid=l+r>>1;
        nw(x,mid,fa);
        build(ch[x][0],l,mid-1,x);
        build(ch[x][1],mid+1,r,x);
        up(x);
    }
    inline int find(int _key) //返回值为key的节点 若无返回0 若有将其转移到根处
    {
        if(!root)return 0;
        int x=root;
        for(pd(x);x&&key[x]!=_key;)x=ch[x][key[x]<_key];
        if(x)splay(x,0);
        return x;
    }
	inline void Delete(int r)//删除下标为r的节点
	{
        splay(r,0);
        int pos=sz[ch[r][0]];
		splay(kth(pos),0);
		splay(kth(pos+2),root);
        ch[ch[root][1]][0]=0;
        up(ch[root][1]),up(root);
    }
    inline int kth(int k)//获得第k小
    {
        if(k>sz[root]||k<=0)return 0;
        int x=root,tmp;
        while(1)
        {
            pd(x),tmp=sz[ch[x][0]]+1;
            if(k==tmp)break;
            if(k<tmp)x=ch[x][0];else k-=tmp,x=ch[x][1];
        }
        return x;
    }
    void reverse(int a,int b)//翻转a到b区间
    {
        splay(kth(a),0),splay(kth(b+2),root);
        rev1(ch[ch[root][1]][0]);
    }
    inline void ins(int _key)//插入key键值
    {
        if(!root){nw(root,_key,0);return;}
        int x=root,y=0;
        while(x)pd(x),y=x,sz[x]++,x=ch[x][key[x]<_key];
        nw(ch[y][key[y]<_key],_key,y);
        splay(ch[y][key[y]<_key],0);
    }
    void cut(int a,int b,int c)
    {
        splay(kth(a),0),splay(kth(b+2),root);
        int tmp=ch[ch[root][1]][0];
        ch[ch[root][1]][0]=0;
        pd(ch[root][1]),pd(root);
        splay(kth(c+1),0);
        splay(kth(c+2),root);
        ch[ch[root][1]][0]=tmp;
        f[ch[ch[root][1]][0]]=ch[root][1];
        up(ch[root][1]),up(root);
    }
	void inorder(int r)//按顺序输出
	{
		if(!r)return;
		inorder(ch[r][0]);
		printf("%d ",key[r]);
		inorder(ch[r][1]);
	}
}spt;
//--------------------------------

int n,now,f[N],ed;
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x!=b.x?x<b.x:idx<b.idx;}
}a[N];
set<node>st;
set<node>::iterator it;
int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getkth(int x)
{
	int rt=spt.find(x);
	return spt.sz[spt.ch[rt][0]]+1;
}

int del()
{
	it=st.lower_bound({a[ed].x,now});
	if(it==st.end()||it->x > a[ed].x)
	{
		it=st.lower_bound({a[ed].x,1});
	}
	int tmp=it->idx;
	st.erase(it);
	return tmp;
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)scanf("%d",&a[i].x),a[i].idx=i,f[i]=i,st.insert(a[i]);
	sort(a+1,a+1+n);
	//F(i,1,n)printf("a[%d].x=%d,idx=%d\n",i,a[i].x,a[i].idx);
	spt.build(spt.root,1,n+2);
	now=1,ed=1;
	long long ans=0;
	while(ed<=n)
	{
		//printf("ed=%d,now=%d\n",ed,now);
		int cur=getkth(now+1),tp=del();
		int tmp=getkth(tp+1);
		//printf("cur=%d,tmp=%d,ed=%d,idx=%d\n",cur,tmp,ed,tp);
		if(tmp>=cur)ans+=tmp-cur+1;
		else
		{
			//printf("sz=%d\n",spt.sz[spt.root]);
			ans+=spt.sz[spt.root]-cur+tmp-1;
		}
		int rt=spt.find(tp+1);
		//printf("rt=%d\n",rt);
		spt.Delete(rt);
		//printf("ans=%lld\n",ans);
		if(tp<n)f[tp]=tp+1;
		else f[tp]=1;
		if(ed<n)now=find(f[tp]);
		ed++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
#define ___ freopen("c:\\code\\in.txt","r",stdin);
const int N=1e6+7;
int _t;
//键值可重，去重需要加num记录这个数的个数
//有区间翻转时建树的数组下标全部加1,建树要建n+2个节点
struct Splay_tree
{
    int root,q[N];bool rev[N];
    int key[N],sz[N],f[N],ch[N][2],add[N];
    void init(){root=_t=0;}//初始化为一棵空树
    void rev1(int x){if(x)swap(ch[x][0],ch[x][1]),rev[x]^=1;}
    void add1(int x,int p){if(x)key[x]+=p,add[x]+=p;}
    inline void nw(int &x,int val,int fa)
    {
        x=++_t,key[x]=val,f[x]=fa,sz[x]=1;
        ch[x][0]=ch[x][1]=0;
        add[x]=rev[x]=0;
    }
    inline void pd(int x)
    {
        if(add[x])add1(ch[x][0],add[x]),add1(ch[x][1],add[x]),add[x]=0;
        if(rev[x])rev1(ch[x][0]),rev1(ch[x][1]),rev[x]=0;
    }
    inline void up(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
    void rotate(int x){
        int y=f[x],w=ch[y][1]==x;
        ch[y][w]=ch[x][w^1];
        if(ch[x][w^1])f[ch[x][w^1]]=y;
        if(f[y]){
            int z=f[y];
            if(ch[z][0]==y)ch[z][0]=x;
            if(ch[z][1]==y)ch[z][1]=x;
        }
        f[x]=f[y],ch[x][w^1]=y,f[y]=x,up(y);
    }

    void splay(int x,int w){
        int s=1,i=x,y;q[1]=x;
        while(f[i])q[++s]=i=f[i];
        while(s)pd(q[s--]);
        while(f[x]!=w){
            y=f[x];
            if(f[y]!=w){if((ch[f[y]][0]==y)^(ch[y][0]==x))rotate(x);else rotate(y);}
            rotate(x);
        }
        if(!w)root=x;
        up(x);
    }
    void build(int &x,int l,int r,int fa=0)//按照数组下标建树
    {
        if(l>r)return;
        int mid=l+r>>1;
        nw(x,mid,fa);
        build(ch[x][0],l,mid-1,x);
        build(ch[x][1],mid+1,r,x);
        up(x);
    }
    inline int find(int _key) //返回值为key的节点 若无返回0 若有将其转移到根处
    {
        if(!root)return 0;
        int x=root;
        for(pd(x);x&&key[x]!=_key;)x=ch[x][key[x]<_key];
        if(x)splay(x,0);
        return x;
    }
	inline void Delete(int r)//删除下标为r的节点
	{
        splay(r,0);
        int pos=sz[ch[r][0]];
		splay(kth(pos),0);
		splay(kth(pos+2),root);
        ch[ch[root][1]][0]=0;
        up(ch[root][1]),up(root);
    }
    inline int kth(int k)//获得第k小
    {
        if(k>sz[root]||k<=0)return 0;
        int x=root,tmp;
        while(1)
        {
            pd(x),tmp=sz[ch[x][0]]+1;
            if(k==tmp)break;
            if(k<tmp)x=ch[x][0];else k-=tmp,x=ch[x][1];
        }
        return x;
    }
    void reverse(int a,int b)//翻转a到b区间
    {
        splay(kth(a),0),splay(kth(b+2),root);
        rev1(ch[ch[root][1]][0]);
    }
    inline void ins(int _key)//插入key键值
    {
        if(!root){nw(root,_key,0);return;}
        int x=root,y=0;
        while(x)pd(x),y=x,sz[x]++,x=ch[x][key[x]<_key];
        nw(ch[y][key[y]<_key],_key,y);
        splay(ch[y][key[y]<_key],0);
    }
    void cut(int a,int b,int c)
    {
        splay(kth(a),0),splay(kth(b+2),root);
        int tmp=ch[ch[root][1]][0];
        ch[ch[root][1]][0]=0;
        pd(ch[root][1]),pd(root);
        splay(kth(c+1),0);
        splay(kth(c+2),root);
        ch[ch[root][1]][0]=tmp;
        f[ch[ch[root][1]][0]]=ch[root][1];
        up(ch[root][1]),up(root);
    }
	void inorder(int r)//按顺序输出
	{
		if(!r)return;
		inorder(ch[r][0]);
		printf("%d ",key[r]);
		inorder(ch[r][1]);
	}
}spt;
//--------------------------------

int n,now,f[N],ed;
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x<b.x;}
}a[N];

int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getkth(int x)
{
	int rt=spt.find(x);
	return spt.sz[spt.ch[rt][0]]+1;
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)scanf("%d",&a[i].x),a[i].idx=i,f[i]=i;
	sort(a+1,a+1+n);
	spt.build(spt.root,1,n+2);
	now=1,ed=1;
	long long ans=0;
	while(ed<=n)
	{
		//printf("ed=%d,now=%d\n",ed,now);
		int cur=getkth(now+1);
		int tmp=getkth(a[ed].idx+1);
		//printf("cur=%d,tmp=%d\n",cur,tmp);
		if(tmp>=cur)ans+=tmp-cur+1;
		else ans+=spt.sz[spt.root]-2-tmp+1+cur;
		int rt=spt.find(a[ed].idx+1);
		//printf("rt=%d\n",rt);
		spt.Delete(rt);
		//printf("ans=%lld\n",ans);
		if(a[ed].idx<n)f[a[ed].idx]=a[ed].idx+1;
		else f[a[ed].idx]=1;
		if(ed<n)now=find(f[a[ed].idx]);
		ed++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define RT(l,r) (l+r|l!=r)
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;

const int N=1e6+7;
int n,now,f[N],cnt,x,sum[N*2];
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x!=b.x?x<b.x:idx<b.idx;}
};
set<node>st;
set<node>::iterator it;

int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getnxt()
{
	int X=st.begin()->x;
	it=st.lower_bound({X,now});
	if(it==st.end()||it->x > X)
		it=st.lower_bound({X,1});
	int tmp=it->idx;
	st.erase(it);
	return tmp;
}

void update(int pos,int v,int l=1,int r=n)
{
	int rt=RT(l,r),m=l+r>>1;
	if(l==r){sum[rt]+=v;return;}
	if(pos<=m)update(pos,v,l,m);
	else update(pos,v,m+1,r);
	sum[rt]=sum[RT(l,m)]+sum[RT(m+1,r)];
}

int query(int L,int R,int l=1,int r=n)
{
	int rt=RT(l,r),m=l+r>>1,an=0;
	if(L<=l&&r<=R)return sum[rt];
	if(L<=m)an+=query(L,R,l,m);
	if(R>m)an+=query(L,R,m+1,r);
	return an;
}

int ask(int a,int b)
{
	if(a<=b)return query(a,b);
	else return query(a,n)+query(1,b);
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)
	{
		scanf("%d",&x),f[i]=i;
		st.insert({x,i}),update(i,1);
	}
	now=1,cnt=1;
	long long ans=0;
	while(cnt<=n)
	{
		int nxt=getnxt();
		ans+=ask(now,nxt);
		if(nxt<n)f[nxt]=nxt+1;else f[nxt]=1;
		if(cnt<n)now=find(nxt);
		update(nxt,-1);
		cnt++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
#define ___ freopen("c:\\code\\in.txt","r",stdin);
const int N=1e6+7;
int _t;
//键值可重，去重需要加num记录这个数的个数
//有区间翻转时建树的数组下标全部加1,建树要建n+2个节点
struct Splay_tree
{
    int root,q[N];bool rev[N];
    int key[N],sz[N],f[N],ch[N][2],add[N];
    void init(){root=_t=0;}//初始化为一棵空树
    void rev1(int x){if(x)swap(ch[x][0],ch[x][1]),rev[x]^=1;}
    void add1(int x,int p){if(x)key[x]+=p,add[x]+=p;}
    inline void nw(int &x,int val,int fa)
    {
        x=++_t,key[x]=val,f[x]=fa,sz[x]=1;
        ch[x][0]=ch[x][1]=0;
        add[x]=rev[x]=0;
    }
    inline void pd(int x)
    {
        if(add[x])add1(ch[x][0],add[x]),add1(ch[x][1],add[x]),add[x]=0;
        if(rev[x])rev1(ch[x][0]),rev1(ch[x][1]),rev[x]=0;
    }
    inline void up(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
    void rotate(int x){
        int y=f[x],w=ch[y][1]==x;
        ch[y][w]=ch[x][w^1];
        if(ch[x][w^1])f[ch[x][w^1]]=y;
        if(f[y]){
            int z=f[y];
            if(ch[z][0]==y)ch[z][0]=x;
            if(ch[z][1]==y)ch[z][1]=x;
        }
        f[x]=f[y],ch[x][w^1]=y,f[y]=x,up(y);
    }

    void splay(int x,int w){
        int s=1,i=x,y;q[1]=x;
        while(f[i])q[++s]=i=f[i];
        while(s)pd(q[s--]);
        while(f[x]!=w){
            y=f[x];
            if(f[y]!=w){if((ch[f[y]][0]==y)^(ch[y][0]==x))rotate(x);else rotate(y);}
            rotate(x);
        }
        if(!w)root=x;
        up(x);
    }
    void build(int &x,int l,int r,int fa=0)//按照数组下标建树
    {
        if(l>r)return;
        int mid=l+r>>1;
        nw(x,mid,fa);
        build(ch[x][0],l,mid-1,x);
        build(ch[x][1],mid+1,r,x);
        up(x);
    }
    inline int find(int _key) //返回值为key的节点 若无返回0 若有将其转移到根处
    {
        if(!root)return 0;
        int x=root;
        for(pd(x);x&&key[x]!=_key;)x=ch[x][key[x]<_key];
        if(x)splay(x,0);
        return x;
    }
	inline void Delete(int r)//删除下标为r的节点
	{
        splay(r,0);
        int pos=sz[ch[r][0]];
		splay(kth(pos),0);
		splay(kth(pos+2),root);
        ch[ch[root][1]][0]=0;
        up(ch[root][1]),up(root);
    }
    inline int kth(int k)//获得第k小
    {
        if(k>sz[root]||k<=0)return 0;
        int x=root,tmp;
        while(1)
        {
            pd(x),tmp=sz[ch[x][0]]+1;
            if(k==tmp)break;
            if(k<tmp)x=ch[x][0];else k-=tmp,x=ch[x][1];
        }
        return x;
    }
    void reverse(int a,int b)//翻转a到b区间
    {
        splay(kth(a),0),splay(kth(b+2),root);
        rev1(ch[ch[root][1]][0]);
    }
    inline void ins(int _key)//插入key键值
    {
        if(!root){nw(root,_key,0);return;}
        int x=root,y=0;
        while(x)pd(x),y=x,sz[x]++,x=ch[x][key[x]<_key];
        nw(ch[y][key[y]<_key],_key,y);
        splay(ch[y][key[y]<_key],0);
    }
    void cut(int a,int b,int c)
    {
        splay(kth(a),0),splay(kth(b+2),root);
        int tmp=ch[ch[root][1]][0];
        ch[ch[root][1]][0]=0;
        pd(ch[root][1]),pd(root);
        splay(kth(c+1),0);
        splay(kth(c+2),root);
        ch[ch[root][1]][0]=tmp;
        f[ch[ch[root][1]][0]]=ch[root][1];
        up(ch[root][1]),up(root);
    }
	void inorder(int r)//按顺序输出
	{
		if(!r)return;
		inorder(ch[r][0]);
		printf("%d ",key[r]);
		inorder(ch[r][1]);
	}
}spt;
//--------------------------------

int n,now,f[N],ed;
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x<b.x;}
}a[N];

int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getkth(int x)
{
	int rt=spt.find(x);
	return spt.sz[spt.ch[rt][0]]+1;
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)scanf("%d",&a[i].x),a[i].idx=i,f[i]=i;
	sort(a+1,a+1+n);
	spt.build(spt.root,1,n+2);
	now=1,ed=1;
	long long ans=0;
	while(ed<=n)
	{
		//printf("ed=%d,now=%d\n",ed,now);
		int cur=getkth(now+1);
		int tmp=getkth(a[ed].idx+1);
		//printf("cur=%d,tmp=%d\n",cur,tmp);
		if(tmp>=cur)ans+=tmp-cur+1;
		else ans+=spt.sz[spt.root]-cur+tmp-1;
		int rt=spt.find(a[ed].idx+1);
		//printf("rt=%d\n",rt);
		spt.Delete(rt);
		//printf("ans=%lld\n",ans);
		if(a[ed].idx<n)f[a[ed].idx]=a[ed].idx+1;
		else f[a[ed].idx]=1;
		if(ed<n)now=find(f[a[ed].idx]);
		ed++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define RT(l,r) (l+r|l!=r)
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
#define ___ freopen("c:\\code\\in.txt","r",stdin);
const int N=1e6+7;
int n,now,f[N],cnt,x,sum[N*2];
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x!=b.x?x<b.x:idx<b.idx;}
};
set<node>st;
set<node>::iterator it;

int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getnxt()
{
	int X=st.begin()->x;
	it=st.lower_bound({X,now});
	if(it==st.end()||it->x > X)
		it=st.lower_bound({X,1});
	int tmp=it->idx;
	st.erase(it);
	return tmp;
}

void update(int pos,int v,int l=1,int r=n)
{
	int rt=RT(l,r),m=l+r>>1;
	if(l==r){sum[rt]+=v;return;}
	if(pos<=m)update(pos,v,l,m);
	else update(pos,v,m+1,r);
	sum[rt]=sum[RT(l,m)]+sum[RT(m+1,r)];
}

int query(int L,int R,int l=1,int r=n)
{
	int rt=RT(l,r),m=l+r>>1,an=0;
	if(L<=l&&r<=R)return sum[rt];
	if(L<=m)an+=query(L,R,l,m);
	if(R>m)an+=query(L,R,m+1,r);
	return an;
}

int ask(int a,int b)
{
	if(a<=b)return query(a,b);
	else
	{
		int tpa=query(a,n);
		int tpb=query(1,b);
		return tpa+tpb;
	}
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)
	{
		scanf("%d",&x),f[i]=i;
		st.insert({x,i}),update(i,1);
	}
	now=1,cnt=1;
	long long ans=0;
	while(cnt<=n)
	{
		int nxt=getnxt();
		ans+=ask(now,nxt);
		if(nxt<n)f[nxt]=nxt+1;else f[nxt]=1;
		if(cnt<n)now=find(nxt);
		update(nxt,-1);
		cnt++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int N=1e6+7;
int _t;
struct Splay_tree
{
    int root,q[N];
    int key[N],sz[N],f[N],ch[N][2],add[N];
    void init(){root=_t=0;}//初始化为一棵空树
    inline void nw(int &x,int val,int fa)
    {
        x=++_t,key[x]=val,f[x]=fa,sz[x]=1;
        ch[x][0]=ch[x][1]=0;
    }
    inline void pd(int x){}
    inline void up(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
    void rotate(int x){
        int y=f[x],w=ch[y][1]==x;
        ch[y][w]=ch[x][w^1];
        if(ch[x][w^1])f[ch[x][w^1]]=y;
        if(f[y]){
            int z=f[y];
            if(ch[z][0]==y)ch[z][0]=x;
            if(ch[z][1]==y)ch[z][1]=x;
        }
        f[x]=f[y],ch[x][w^1]=y,f[y]=x,up(y);
    }

    void splay(int x,int w){
        int s=1,i=x,y;q[1]=x;
        while(f[i])q[++s]=i=f[i];
        while(s)pd(q[s--]);
        while(f[x]!=w){
            y=f[x];
            if(f[y]!=w){if((ch[f[y]][0]==y)^(ch[y][0]==x))rotate(x);else rotate(y);}
            rotate(x);
        }
        if(!w)root=x;
        up(x);
    }
    void build(int &x,int l,int r,int fa=0)//按照数组下标建树
    {
        if(l>r)return;
        int mid=l+r>>1;
        nw(x,mid,fa);
        build(ch[x][0],l,mid-1,x);
        build(ch[x][1],mid+1,r,x);
        up(x);
    }
    inline int find(int _key) //返回值为key的节点 若无返回0 若有将其转移到根处
    {
        if(!root)return 0;
        int x=root;
        for(pd(x);x&&key[x]!=_key;)x=ch[x][key[x]<_key];
        if(x)splay(x,0);
        return x;
    }
	inline void Delete(int r)//删除下标为r的节点
	{
        splay(r,0);
        int pos=sz[ch[r][0]];
		splay(kth(pos),0);
		splay(kth(pos+2),root);
        ch[ch[root][1]][0]=0;
        up(ch[root][1]),up(root);
    }
    inline int kth(int k)//获得第k小
    {
        if(k>sz[root]||k<=0)return 0;
        int x=root,tmp;
        while(1)
        {
            pd(x),tmp=sz[ch[x][0]]+1;
            if(k==tmp)break;
            if(k<tmp)x=ch[x][0];else k-=tmp,x=ch[x][1];
        }
        return x;
    }
}spt;
//--------------------------------

int n,now,f[N],ed;
struct node
{
	int x,idx;
	bool operator <(const node &b)const{return x!=b.x?x<b.x:idx<b.idx;}
}a[N];
set<node>st;
set<node>::iterator it;
int find(int x){return f[x]!=x?f[x]=find(f[x]):x;}

int getkth(int x)
{
	int rt=spt.find(x);
	return spt.sz[spt.ch[rt][0]]+1;
}

int del()
{
	it=st.lower_bound({a[ed].x,now});
	if(it==st.end()||it->x > a[ed].x)
	{
		it=st.lower_bound({a[ed].x,1});
	}
	int tmp=it->idx;
	st.erase(it);
	return tmp;
}

int main()
{
	scanf("%d",&n);
	F(i,1,n)scanf("%d",&a[i].x),a[i].idx=i,f[i]=i,st.insert(a[i]);
	sort(a+1,a+1+n);
	spt.build(spt.root,1,n+2);
	now=1,ed=1;
	long long ans=0;
	while(ed<=n)
	{
		int cur=getkth(now+1),tp=del();
		int tmp=getkth(tp+1);
		if(tmp>=cur)ans+=tmp-cur+1;
		else ans+=spt.sz[spt.root]-cur+tmp-1;
		int rt=spt.find(tp+1);
		spt.Delete(rt);
		if(tp<n)f[tp]=tp+1;
		else f[tp]=1;
		if(ed<n)now=find(f[tp]);
		ed++;
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int N, mx, x, c = 1, A; int64_t R;
vector<int> v[100017];
int main()
{
    scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A), v[A].push_back(i), mx = max(mx, A);
    for (int i = 1; i <= mx; i++)
        if (!v[i].empty()) {
            vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), x);
            R += 1LL * c * (v[i].end() - it);
            x = v[i].back();
            v[i].erase(it, v[i].end());
            if (!v[i].empty()) c++, i--, x = 0;
        }
    printf("%I64d\n", R);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 17;

int N, maxv;
long long R;
vector<int> v[maxn];

int main()
{
    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        int A;
        scanf("%d", &A);
        v[A].push_back(i);
        maxv = max(maxv, A);
    }

    int x = 0, count = 1;

    for (int i = 1; i <= maxv; i++)
        if (!v[i].empty()) {
            vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), x);
            R += 1LL * count * (v[i].end() - it);
            x = v[i].back();
            v[i].erase(it, v[i].end());
            if (!v[i].empty()) count++, i--, x = 0;
        }

    printf("%I64d\n", R);

    return 0;
}


#include <bits/stdc++.h>

#define pb push_back

using namespace std;

const int maxn = 1e5 + 17;

int N, R, maxv;
vector<int> card[maxn];

int main()
{
    ios_base::sync_with_stdio(false);

    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        int A;
        scanf("%d", &A);
        card[A].pb(i);
        maxv = max(maxv, A);
    }

    int x = 0, count = 1;

    for (int i = 1; i <= maxv; i++) {
        if (card[i].empty()) continue;

        vector<int>::iterator it = lower_bound(card[i].begin(), card[i].end(), x);

        R += count * (card[i].end() - it);
        x = card[i].back();
        card[i].erase(it, card[i].end());

        if (!card[i].empty()) count++, i--, x = 0;
    }

    cout << R << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int N, mx, x, c = 1, A; int64_t R;
vector<int> v[100017];
int main()
{
    scanf("%d", &N), for (int i = 0; i < N; i++) scanf("%d", &A), v[A].push_back(i), mx = max(mx, A);
    for (int i = 1; i <= mx; i++)
        if (!v[i].empty()) {
            vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), x);
            R += 1LL * c * (v[i].end() - it);
            x = v[i].back();
            v[i].erase(it, v[i].end());
            if (!v[i].empty()) c++, i--, x = 0;
        }
    printf("%I64d\n", R);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N, mx, x, c = 1, A;
int64_t R;
vector<int> v[100017];

int main()
{
    scanf("%d", &N);

    for (int i = 0; i < N; i++) scanf("%d", &A), v[A].push_back(i), mx = max(mx, A);

    for (int i = 1; i <= mx; i++)
        if (!v[i].empty()) {
            vector<int>::iterator it = lower_bound(v[i].begin(), v[i].end(), x);
            R += 1LL * c * (v[i].end() - it);
            x = v[i].back();
            v[i].erase(it, v[i].end());
            if (!v[i].empty()) c++, i--, x = 0;
        }

    printf("%I64d\n", R);

    return 0;
}


#include <bits/stdc++.h>

#define pb push_back

using namespace std;

const int maxn = 1e5 + 17;

int N, maxv;
long long R;
vector<int> card[maxn];

int main()
{
    ios_base::sync_with_stdio(false);

    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        int A;
        scanf("%d", &A);
        card[A].pb(i);
        maxv = max(maxv, A);
    }

    int x = 0, count = 1;

    for (int i = 1; i <= maxv; i++) {
        if (card[i].empty()) continue;

        vector<int>::iterator it = lower_bound(card[i].begin(), card[i].end(), x);

        R += 1LL * count * (card[i].end() - it);
        x = card[i].back();
        card[i].erase(it, card[i].end());

        if (!card[i].empty()) count++, i--, x = 0;
    }

    cout << R << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N, mx, x, count = 1, A;
long long R;
vector<int> v[100017];

int main()
{
    scanf("%d", &N);

    for (int i = 0; i < N; i++) scanf("%d", &A), v[A].push_back(i), mx = max(mx, A);

    for (int i = 1; i <= mx; i++)
        if (!v[i].empty()) {
            auto it = lower_bound(v[i].begin(), v[i].end(), x);
            R += 1LL * count * (v[i].end() - it);
            x = v[i].back();
            v[i].erase(it, v[i].end());
            if (!v[i].empty()) count++, i--, x = 0;
        }

    printf("%I64d\n", R);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

struct Node {
	Node *l, *r;
	int prior;
	int val;
	int minVal;
	int size;
	Node(int x) {
		l = NULL;
		r = NULL;
		prior = rand();
		val = x;
		minVal = x;
		size = 1;
	}
};

int getMin(Node *v) {
	if (v == NULL) return 100 * 1000 + 7;
	return v->minVal;
}

int getSize(Node *v) {
	if (v == NULL) return 0;
	return v->size;
}

void upd(Node *v) {
	v->size = getSize(v->l) + getSize(v->r) + 1;
	v->minVal = min(min(getMin(v->l), getMin(v->r)), v->val);
}

int getMinPos(Node *v) {
	if (getMin(v->r) == getMin(v)) return getSize(v->l) + 1 + getMinPos(v->r);
	if (getMin(v) == v->val) return getSize(v->l);
	return getMinPos(v->l);
}

pair<Node*, Node*> split(Node *v, int x) {
	if (v == NULL) return make_pair(v, v);
	int id = getSize(v->l) + 1;
	if (id <= x) {
		pair<Node*, Node*> tmp = split(v->r, x - id);
		v->r = tmp.first;
		upd(v);
		return make_pair(v, tmp.second);
	} else {
		pair<Node*, Node*> tmp = split(v->l, x);
		v->l = tmp.second;
		upd(v);
		return make_pair(tmp.first, v);
	}
}

Node* merge(Node *l, Node *r) {
	if (l == NULL) return r;
	if (r == NULL) return l;
	if (l->prior < r->prior) {
		l->r = merge(l->r, r);
		upd(l);
		return l;
	} else {
		r->l = merge(l, r->l);
		upd(r);
		return r;
	}
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	int n;
	scanf("%d", &n);
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	Node *t = NULL;
	for (int i = 0; i < n; i++) {
		t = merge(t,  new Node(a[n - 1 - i]));
	}
	ll res = 0;
	for (int i = 0; i < n; i++) {
		int pos = getMinPos(t) + 1;
	//	cerr << pos << endl;
		pair<Node*, Node*> tmp = split(t, pos);
		res += getSize(tmp.second) + 1;
		pair<Node*, Node*> tmp2 = split(tmp.first, pos - 1);
		t = merge(tmp.second, tmp2.first);
	}
	printf("%lld\n", res);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

struct Node {
	Node *l, *r;
	int prior;
	int val;
	int minVal;
	int size;
	Node(int x) {
		l = NULL;
		r = NULL;
		prior = rand();
		val = x;
		minVal = x;
		size = 1;
	}
};

int getMin(Node *v) {
	if (v == NULL) return 100 * 1000 + 7;
	return v->minVal;
}

int getSize(Node *v) {
	if (v == NULL) return 0;
	return v->size;
}

void upd(Node *v) {
	v->size = getSize(v->l) + getSize(v->r) + 1;
	v->minVal = min(min(getMin(v->l), getMin(v->r)), v->val);
}

int getMinPos(Node *v) {
	if (getMin(v->r) == getMin(v)) return getSize(v->l) + 1 + getMinPos(v->r);
	if (getMin(v) == v->val) return getSize(v->l);
	return getMinPos(v->l);
}

pair<Node*, Node*> split(Node *v, int x) {
	if (v == NULL) return make_pair(v, v);
	int id = getSize(v->l) + 1;
	if (id <= x) {
		pair<Node*, Node*> tmp = split(v->r, x - id);
		v->r = tmp.first;
		upd(v);
		return make_pair(v, tmp.second);
	} else {
		pair<Node*, Node*> tmp = split(v->l, x);
		v->l = tmp.second;
		upd(v);
		return make_pair(tmp.first, v);
	}
}

Node* merge(Node *l, Node *r) {
	if (l == NULL) return r;
	if (r == NULL) return l;
	if (l->prior < r->prior) {
		l->r = merge(l->r, r);
		upd(l);
		return l;
	} else {
		r->l = merge(l, r->l);
		upd(r);
		return r;
	}
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	int n;
	scanf("%d", &n);
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	Node *t = NULL;
	for (int i = 0; i < n; i++) {
		t = merge(t,  new Node(n - 1 - i));
	}
	ll res = 0;
	for (int i = 0; i < n; i++) {
		int pos = getMinPos(t) + 1;
	//	cerr << pos << endl;
		pair<Node*, Node*> tmp = split(t, pos);
		res += getSize(tmp.second) + 1;
		pair<Node*, Node*> tmp2 = split(tmp.first, pos - 1);
		t = merge(tmp.second, tmp2.first);
	}
	printf("%lld\n", res);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

struct Node {
	Node *l, *r;
	int prior;
	int val;
	int minVal;
	int size;
	Node(int x) {
		l = NULL;
		r = NULL;
		prior = rand();
		val = x;
		minVal = x;
		size = 1;
	}
};

int getMin(Node *v) {
	if (v == NULL) return 100 * 1000 + 7;
	return v->minVal;
}

int getSize(Node *v) {
	if (v == NULL) return 0;
	return v->size;
}

void upd(Node *v) {
	v->size = getSize(v->l) + getSize(v->r) + 1;
	v->minVal = min(min(getMin(v->l), getMin(v->r)), v->val);
}

int getMinPos(Node *v) {
	if (getMin(v->r) == getMin(v)) return getSize(v->l) + 1 + getMinPos(v->r);
	if (getMin(v) == v->val) return getSize(v->l);
	return getMinPos(v->l);
}

pair<Node*, Node*> split(Node *v, int x) {
	if (v == NULL) return make_pair(v, v);
	int id = getSize(v->l) + 1;
	if (id <= x) {
		pair<Node*, Node*> tmp = split(v->r, x - id);
		v->r = tmp.first;
		upd(v);
		return make_pair(v, tmp.second);
	} else {
		pair<Node*, Node*> tmp = split(v->l, x);
		v->l = tmp.second;
		upd(v);
		return make_pair(tmp.first, v);
	}
}

Node* merge(Node *l, Node *r) {
	if (l == NULL) return r;
	if (r == NULL) return l;
	if (l->prior < r->prior) {
		l->r = merge(l->r, r);
		upd(l);
		return l;
	} else {
		r->l = merge(l, r->l);
		upd(r);
		return r;
	}
}

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	int n;
	scanf("%d", &n);
	Node *t = NULL;
	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		t = merge(t,  new Node(x));
	}
	ll res = 0;
	for (int i = 0; i < n; i++) {
		int pos = getMinPos(t) + 1;
	//	cerr << pos << endl;
		pair<Node*, Node*> tmp = split(t, pos);
		res += getSize(tmp.second) + 1;
		pair<Node*, Node*> tmp2 = split(tmp.first, pos - 1);
		t = merge(tmp.second, tmp2.first);
	}
	printf("%lld\n", res);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<utility>
#include<vector>

using namespace std;

vector<int>  a[100005];
typedef long long LL;

int main() {
    int n = 0,
        x = 0,
        maxV = 0;

    scanf("%d", &n);
    for(int i = 0; i<n; ++i) {
        scanf("%d", &x);
        a[x].push_back(i);
        maxV = max(maxV, x);
    }

    LL  res = 1;
    int last = 0;
    vector<int> moved;
    for(int i = 1; i<=maxV; ++i) {
        if(a[i].empty()) {
            continue;
        }

        auto p = lower_bound(a[i].begin(), a[i].end(), last);
        int idx = 0;
        if(p == a[i].end()) {
            idx = 0;
        }
        else {
            idx = p-a[i].begin();
        }

        int mv = 0,
            len = a[i].size();
        while(mv<len) {
            int cur = a[i][idx%len];
            int lidx = lower_bound(moved.begin(), moved.end(), last+1)-moved.begin(),
                cidx = lower_bound(moved.begin(), moved.end(), cur)-moved.begin();
            if(cur>=last) {
                res += LL(cur-last-(cidx-lidx));
            }
            else {
                res += (LL)(n-last+cur-(moved.size()+cidx-lidx));
            }
            moved.insert(moved.begin()+cidx, cur);
            last = cur;
            ++idx;
            ++mv;
        }
    }

    printf("%I64d", res);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<memory.h>

using namespace std;

int     a[100005],
        moved[100005],
        b[100005];
typedef long long LL;

int main() {
    int n = 0,
        x = 0;

    memset(moved, 0, sizeof(moved));
    scanf("%d", &n);
    multiset<int>   rest;
    for(int i = 0; i<n; ++i) {
        scanf("%d", &x);
        a[i] = x;
        b[i] = x;
    }

    sort(b, b+n);
    LL  res = 0;
    int cur = 0,
        bcur = 0;
    while(bcur<n) {
        int tmp = b[bcur];
        ++bcur;
        while(true) {
            cur %= n;
            if(moved[cur]) {
                ++cur;
            }
            else if(a[cur] == tmp) {
                ++res;
                moved[cur] = 1;
                ++cur;
                break;
            }
            else {
                ++res;
                ++cur;
            }
        }
    }

    printf("%I64d", res);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<memory.h>

using namespace std;

int     a[100005],
        moved[100005],
        b[100005];
typedef long long LL;

int main() {
    int n = 0,
        x = 0;

    memset(moved, 0, sizeof(moved));
    scanf("%d", &n);
    multiset<int>   rest;
    for(int i = 0; i<n; ++i) {
        scanf("%d", &x);
        a[i] = x;
        b[i] = x;
    }

    sort(b, b+n);
    LL  res = 0;
    int cur = 0,
        bcur = 0;
    while(bcur<n) {
        int tmp = b[bcur];
        ++bcur;
        while(true) {
            if(moved[cur]) {
                ++cur;
            }
            else if(a[cur] == tmp) {
                ++res;
                moved[cur] = 1;
                ++cur;
                break;
            }
            else {
                ++res;
                ++cur;
            }
            if(cur>=n) {
                cur %= n;
            }
        }
    }

    printf("%I64d", res);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<utility>
#include<vector>

using namespace std;

vector<int>  a[100005];
typedef long long LL;

int main() {
    int n = 0,
        x = 0,
        maxV = 0;

    scanf("%d", &n);
    for(int i = 0; i<n; ++i) {
        scanf("%d", &x);
        a[x].push_back(i);
        maxV = max(maxV, x);
    }

    LL  res = 0,
        rnd = 1;
    int last = 0;
    for(int i = 1; i<=maxV; ++i) {
        if(a[i].empty()) {
            continue;
        }

        auto p = lower_bound(a[i].begin(), a[i].end(), last);
        last = a[i].back();

        res += (LL)rnd*(a[i].end()-p);
        a[i].erase(p, a[i].end());
        if(!a[i].empty()) {
            --i;
            ++rnd;
            last = 0;
        }
    }

    printf("%I64d", res);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
struct data
{
    int v,idx;
    data(){}
    data(int _v , int _idx) : v(_v), idx(_idx){}
    bool operator > (const data& cmp) const {return ((v==cmp.v && idx>cmp.idx) || v>cmp.v);}
};
int n,res,ST[1<<18],RSQ(int,int,int,int,int);
void build(int,int,int),update(int,int,int,int);

int main()
{
    priority_queue<data , vector<data> , greater <data> > q;
    scanf("%d",&n);
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        q.push(data(x,i));
    }
    build(0,0,n-1);
    for(int prv=0;!q.empty();)
    {
        int idx=q.top().idx;
        q.pop();
        if(idx>=prv)
            res+=RSQ(0,prv,idx,0,n-1);
        else
            res+=RSQ(0,prv,idx,0,n-1)+RSQ(0,0,idx,0,n-1);
        update(0,0,n-1,idx);
        prv=idx;
    }
    cout<<res;
}
int RSQ(int pos , int ql , int qr , int rl , int rr)
{
    if(rr<ql || rl>qr)
        return 0;
    if(ql<=rl && rr<=qr)
        return ST[pos];
    int mid((rl+rr)/2);
    return RSQ(2*pos+1,ql,qr,rl,mid)+RSQ(2*pos+2,ql,qr,mid+1,rr);
}
void build(int pos , int l , int r)
{
    if(l==r)
    {
        ST[pos]=1;
        return ;
    }
    int mid((l+r)/2);
    build(2*pos+1,l,mid);
    build(2*pos+2,mid+1,r);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}
void update(int pos , int l , int r , int q)
{
    if(q>r || q<l) return;
    if(l==r)
    {
        ST[pos]=0;
        return;
    }
    int mid((l+r)/2);
    update(2*pos+1,l,mid,q);
    update(2*pos+2,mid+1,r,q);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}


#include <bits/stdc++.h>

using namespace std;
typedef set<int>::iterator sii;

set<int> key[100001];
int n,res,ST[1<<18],RSQ(int,int,int,int,int);
void build(int,int,int),update(int,int,int,int);

int main()
{
    priority_queue<int, vector<int> , greater<int> > q;
    scanf("%d",&n);
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        q.push(x);
        key[x].insert(i);
    }
    build(0,0,n-1);
    for(int prv=0;!q.empty();)
    {
        int x=q.top(),idx; q.pop();
        sii iter=key[x].lower_bound(prv);
        if(iter==key[x].end())
            iter=key[x].lower_bound(0);
        idx=*iter;
        key[x].erase(iter);
        if(idx>=prv)
            res+=RSQ(0,prv,idx,0,n-1);
        else
            res+=RSQ(0,prv,n-1,0,n-1)+RSQ(0,0,idx,0,n-1);
        update(0,0,n-1,idx);
        prv=idx;
    }
    cout<<res;
}
int RSQ(int pos , int ql , int qr , int rl , int rr)
{
    if(rr<ql || rl>qr)
        return 0;
    if(ql<=rl && rr<=qr)
        return ST[pos];
    int mid((rl+rr)/2);
    return RSQ(2*pos+1,ql,qr,rl,mid)+RSQ(2*pos+2,ql,qr,mid+1,rr);
}
void build(int pos , int l , int r)
{
    if(l==r)
    {
        ST[pos]=1;
        return ;
    }
    int mid((l+r)/2);
    build(2*pos+1,l,mid);
    build(2*pos+2,mid+1,r);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}
void update(int pos , int l , int r , int q)
{
    if(q>r || q<l) return;
    if(l==r)
    {
        ST[pos]=0;
        return;
    }
    int mid((l+r)/2);
    update(2*pos+1,l,mid,q);
    update(2*pos+2,mid+1,r,q);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef set<ll>::iterator sii;

set<ll> key[100001];
ll n,res,ST[1<<18],RSQ(ll,ll,ll,ll,ll);
void build(ll,ll,ll),update(ll,ll,ll,ll);

int main()
{
    priority_queue<ll, vector<ll> , greater<ll> > q;
    scanf("%I64d",&n);
    for(ll i=0,x;i<n;i++)
    {
        scanf("%I64d",&x);
        q.push(x);
        key[x].insert(i);
    }
    build(0,0,n-1);
    for(ll prv=0;!q.empty();)
    {
        ll x=q.top(),idx; q.pop();
        sii iter=key[x].lower_bound(prv);
        if(iter==key[x].end())
            iter=key[x].lower_bound(0);
        idx=*iter;
        key[x].erase(iter);
        if(idx>=prv)
            res+=RSQ(0,prv,idx,0,n-1);
        else
            res+=RSQ(0,prv,n-1,0,n-1)+RSQ(0,0,idx,0,n-1);
        update(0,0,n-1,idx);
        prv=idx;
    }
    cout<<res;
}
ll RSQ(ll pos , ll ql , ll qr , ll rl , ll rr)
{
    if(rr<ql || rl>qr)
        return 0;
    if(ql<=rl && rr<=qr)
        return ST[pos];
    ll mid((rl+rr)/2);
    return RSQ(2*pos+1,ql,qr,rl,mid)+RSQ(2*pos+2,ql,qr,mid+1,rr);
}
void build(ll pos , ll l , ll r)
{
    if(l==r)
    {
        ST[pos]=1;
        return ;
    }
    ll mid((l+r)/2);
    build(2*pos+1,l,mid);
    build(2*pos+2,mid+1,r);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}
void update(ll pos , ll l , ll r , ll q)
{
    if(q>r || q<l) return;
    if(l==r)
    {
        ST[pos]=0;
        return;
    }
    ll mid((l+r)/2);
    update(2*pos+1,l,mid,q);
    update(2*pos+2,mid+1,r,q);
    ST[pos]=ST[2*pos+1]+ST[2*pos+2];
}


#include <bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define N 100009
#define MOD 1000000007
#define f first
#define s second
#define wt(x) cout << # x <<" = " << x << endl
#define FASTIO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
#define inf 1e15
using namespace std;
void read(bool out = 0){
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
    if(out)
    freopen("out", "wt", stdout);
#endif
}
ll seg [30 * N ] = {} ;
ll from = 0 , to = N ;
void insert ( ll num , ll value , ll s = from , ll  e = to , ll p = 1 )
{
    seg [p] += value  ;
    if ( s == e ) return ;
    ll mid = ( s + e ) >> 1;
    if ( num <= mid ) insert(  num, value , s , mid , p << 1 );
    else insert ( num , value , mid + 1,  e , p << 1 | 1 );
}
ll get ( ll x ,  ll y , ll s= from , ll e = to , ll p = 1 )
{
    if ( x > e || s > y || x > y  ) return 0 ;
    else if ( x <=s && y>=e) return seg [p] ;
    ll mid = ( s + e ) >> 1;
    return get ( x , y, s , mid , p << 1 ) + get ( x ,y , mid + 1, e , p << 1  | 1);
}
ll a [N] ;
vector < ll > place [N] ;
ll men [ N ] ;
int main ()
{
    FASTIO;
    ll n ;
    cin >> n ;
    for ( int i =0 ; i < n ; i ++ )
        cin >> a[i] ;
    for ( int i =0 ; i < n ; i ++ )
        place [a[i]].PB ( i );
    ll ind = 0 ;
    set < ll > st ;
    for ( int i =0 ; i < n ; i ++ )
    {
        st.insert ( a[i] );
    }
    for ( set < ll > :: iterator it = st.begin ( ); it != st.end () ; it ++ )
    {
        men [ind ++ ] = *it ;
    }
    ll last =  - 1;
    ll ans =0 ;
    for ( int card = 0 ; card < ind ; card ++ )
    {
        ll ptr = - 1 ;
        for ( int i =0 ; i < place [ men [ card ]] .size () ;i ++ )
        {
            if ( place [ men [ card ] ] [ i]  > ptr )
            {
                ptr = i ;
                break ;
            }
        }
        if ( ptr == -1 ) ptr = 0 ;
        ll timer = place [ men [ card ] ] . size () ;
        while ( timer -- )
        {
            if ( ptr > last )
            {
                ans += place [men [ card ] ] [ptr] - last - get  ( last + 1  , place [men [ card ]] [ptr]  ) ;
                last = place [men [ card ] ] [ptr]  ;
                insert ( place [men [ card ]] [ptr]  , 1 );
                ptr ++ ;
                ptr %= place [ men [ card ] ] .size () ;
            }
            else
            {
                ans += n - 1 - last - get ( last + 1, n -1  );
                ans += place [men [ card ]] [ptr]  -get ( 1 ,place [men [ card ]] [ptr]  );
                last = place [men [ card ]] [ptr]  ;
                insert (place [men [ card ] ] [ptr] , 1 );
                ptr ++ ;
                ptr %= place [men [ card ] ] .size () ;

            }
        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define N 100009
#define MOD 1000000007
#define f first
#define s second
#define wt(x) cout << # x <<" = " << x << endl
#define FASTIO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
#define inf 1e15
using namespace std;
void read(bool out = 0){
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
    if(out)
    freopen("out", "wt", stdout);
#endif
}
ll seg [30 * N ] = {} ;
ll from = 0 , to = N ;
void insert ( ll num , ll value , ll s = from , ll  e = to , ll p = 1 )
{
    seg [p] += value  ;
    if ( s == e ) return ;
    ll mid = ( s + e ) >> 1;
    if ( num <= mid ) insert(  num, value , s , mid , p << 1 );
    else insert ( num , value , mid + 1,  e , p << 1 | 1 );
}
ll get ( ll x ,  ll y , ll s= from , ll e = to , ll p = 1 )
{
    if ( x > e || s > y || x > y  ) return 0 ;
    else if ( x <=s && y>=e) return seg [p] ;
    ll mid = ( s + e ) >> 1;
    return get ( x , y, s , mid , p << 1 ) + get ( x ,y , mid + 1, e , p << 1  | 1);
}
ll a [N] ;
vector < ll > place [N] ;
ll men [ N ] ;
int main ()
{
    read () ;
    FASTIO;
    ll n ;
    cin >> n ;
    for ( int i =0 ; i < n ; i ++ )
        cin >> a[i] ;
    for ( int i =0 ; i < n ; i ++ )
        place [a[i]].PB ( i );
    ll ind = 0 ;
    set < ll > st ;
    for ( int i =0 ; i < n ; i ++ )
    {
        st.insert ( a[i] );
    }
    for ( set < ll > :: iterator it = st.begin ( ); it != st.end () ; it ++ )
    {
        men [ind ++ ] = *it ;
    }
    ll last =  - 1;
    ll ans =0 ;
    for ( int card = 0 ; card < ind ; card ++ )
    {
        ll ptr = - 1 ;
        for ( int i =0 ; i < place [ men [ card ]] .size () ;i ++ )
        {
            if ( place [ men [ card ] ] [ i]  > last )
            {
                ptr = i ;
                break ;
            }
        }
        if ( ptr == -1 ) ptr = 0 ;
        ll timer = place [ men [ card ] ] . size () ;
        while ( timer -- )
        {
            if ( place [men [ card ]] [ ptr ]  > last )
            {
                ans += place [men [ card ] ] [ptr] - last - get  ( last + 1  , place [men [ card ]] [ptr]  ) ;
                last = place [men [ card ] ] [ptr]  ;
                insert ( place [men [ card ]] [ptr]  , 1 );
                ptr ++ ;
                ptr %= place [ men [ card ] ] .size () ;
            }
            else
            {
                ans += n - 1 - last - get ( last + 1, n -1  );
                ans += place [men [ card ]] [ptr]  -get ( 1 ,place [men [ card ]] [ptr]  ) + 1 ;
                last = place [men [ card ]] [ptr]  ;
                insert (place [men [ card ] ] [ptr] , 1 );
                ptr ++ ;
                ptr %= place [men [ card ] ] .size () ;

            }
        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define N 100009
#define MOD 1000000007
#define f first
#define s second
#define wt(x) cout << # x <<" = " << x << endl
#define FASTIO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
#define inf 1e15
using namespace std;
void read(bool out = 0){
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
    if(out)
    freopen("out", "wt", stdout);
#endif
}
ll seg [30 * N ] = {} ;
ll from = 0 , to = N ;
void insert ( ll num , ll value , ll s = from , ll  e = to , ll p = 1 )
{
    seg [p] += value  ;
    if ( s == e ) return ;
    ll mid = ( s + e ) >> 1;
    if ( num <= mid ) insert(  num, value , s , mid , p << 1 );
    else insert ( num , value , mid + 1,  e , p << 1 | 1 );
}
ll get ( ll x ,  ll y , ll s= from , ll e = to , ll p = 1 )
{
    if ( x > e || s > y || x > y  ) return 0 ;
    else if ( x <=s && y>=e) return seg [p] ;
    ll mid = ( s + e ) >> 1;
    return get ( x , y, s , mid , p << 1 ) + get ( x ,y , mid + 1, e , p << 1  | 1);
}
ll a [N] ;
vector < ll > place [N] ;
ll men [ N ] ;
int main ()
{
    read () ;
    FASTIO;
    ll n ;
    cin >> n ;
    for ( int i =0 ; i < n ; i ++ )
        cin >> a[i] ;
    for ( int i =0 ; i < n ; i ++ )
        place [a[i]].PB ( i );
    ll ind = 0 ;
    set < ll > st ;
    for ( int i =0 ; i < n ; i ++ )
    {
        st.insert ( a[i] );
    }
    for ( set < ll > :: iterator it = st.begin ( ); it != st.end () ; it ++ )
    {
        men [ind ++ ] = *it ;
    }
    ll last =  - 1;
    ll ans =0 ;
    for ( int card = 0 ; card < ind ; card ++ )
    {
        ll ptr = - 1 ;
        for ( int i =0 ; i < place [ men [ card ]] .size () ;i ++ )
        {
            if ( place [ men [ card ] ] [ i]  > last )
            {
                ptr = i ;
                break ;
            }
        }
        if ( ptr == -1 ) ptr = 0 ;
        ll timer = place [ men [ card ] ] . size () ;
        while ( timer -- )
        {
            if ( place [men [ card ]] [ ptr ]  > last )
            {
                ans += place [men [ card ] ] [ptr] - last - get  ( last + 1  , place [men [ card ]] [ptr]  ) ;
                last = place [men [ card ] ] [ptr]  ;
                insert ( place [men [ card ]] [ptr]  , 1 );
                ptr ++ ;
                ptr %= place [ men [ card ] ] .size () ;
            }
            else
            {
                ans += n - 1 - last - get ( last + 1, n -1  );
                ans += place [men [ card ]] [ptr]  -get ( 0 ,place [men [ card ]] [ptr]  ) + 1 ;
                last = place [men [ card ]] [ptr]  ;
                insert (place [men [ card ] ] [ptr] , 1 );
                ptr ++ ;
                ptr %= place [men [ card ] ] .size () ;

            }
        }

    }
    cout << ans ;

}


/*clear adj and visited vector declared globally after each test case
check for long long overflow
while adding and subs check if mod becomes -ve
while using an integer directly in a builtin function add ll
Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
agar RTE aaye to range check karna container ki
container ki size ko variable me store kara kar use karna (nhi to RTE)
remember to put "\n" after every answer in case of multiple testcases
Dont keep array name as size or any other key word*/

// AH! HERE WE GO AGAIN :)
#include<bits/stdc++.h>
#include<string>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll              long long
#define ull             unsigned long long
#define ldb             long long double
#define pll             pair<long long, long long>
#define pb              push_back
#define pf              push_front
#define pof             pop_front
#define pob             pop_back
#define ft              front
#define sz              size
#define gcd             __gcd
#define bk              back
#define ub              upper_bound
#define lb              lower_bound
#define sum             accumulate
#define bs              binary_search
#define fr              first
#define sc              second
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define ins             insert
#define bg              begin
#define rep(i,a,b)      for(i=a;i<=b;i++)
#define repr(i,a,b)     for(i=a;i>=b;i--)
#define Ndigits         floor(log10(N)+1)
#define N               200001
#define M               1003
#define pi              3.141592653589793238
#define FASTIO          std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ordered_set     tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
    FASTIO
    //freopen("input.txt", "r", stdin);   // to be uncommented when I/P
    //freopen("output.txt", "w", stdout); // & O/P are in file format
    ll n,i,m,j,c=0,ans=0;
    cin>>n;
    m=(2*n);
    m--;
    string s;
    cin>>s>>j;
    set<ll> st;
    st.ins(j);
    while(m--)
    {
        cin>>s;
        if(s=="add")
        {
            cin>>j;
            if(j>*st.begin()) c=1;
            st.ins(j);
        }
        else if(s=="remove")
        {
            if(c==1)
            {
                ans++;
                c=0;
            }
            st.erase(st.begin());
        }
    }
    cout<<ans;
    return 0;
}


/*clear adj and visited vector declared globally after each test case
check for long long overflow
while adding and subs check if mod becomes -ve
while using an integer directly in a builtin function add ll
Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
agar RTE aaye to range check karna container ki
container ki size ko variable me store kara kar use karna (nhi to RTE)
remember to put "\n" after every answer in case of multiple testcases
Dont keep array name as size or any other key word*/

// AH! HERE WE GO AGAIN :)
#include<bits/stdc++.h>
#include<string>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll              long long
#define ull             unsigned long long
#define ldb             long long double
#define pll             pair<long long, long long>
#define pb              push_back
#define pf              push_front
#define pof             pop_front
#define pob             pop_back
#define ft              front
#define sz              size
#define gcd             __gcd
#define bk              back
#define ub              upper_bound
#define lb              lower_bound
#define sum             accumulate
#define bs              binary_search
#define fr              first
#define sc              second
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define ins             insert
#define bg              begin
#define rep(i,a,b)      for(i=a;i<=b;i++)
#define repr(i,a,b)     for(i=a;i>=b;i--)
#define Ndigits         floor(log10(N)+1)
#define N               200001
#define M               1003
#define pi              3.141592653589793238
#define FASTIO          std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ordered_set     tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
    FASTIO
    //freopen("input.txt", "r", stdin);   // to be uncommented when I/P
    //freopen("output.txt", "w", stdout); // & O/P are in file format
    ll n,i,m,j,c=0,ans=0;
    cin>>n;
    m=(2*n);
    m--;
    string s;
    cin>>s>>j;
    set<ll> st;
    st.ins(j);
    while(m--)
    {
        cin>>s;
        if(s=="add")
        {
            cin>>j;
            if(j>*st.bg() && c==0) c=1;
            st.ins(j);
        }
        else if(s=="remove")
        {
            if(c==1)
            {
                ans++;
                c=0;
            }
            st.erase(st.begin());
        }
    }
    cout<<ans;
    return 0;
}


/*clear adj and visited vector declared globally after each test case
check for long long overflow
while adding and subs check if mod becomes -ve
while using an integer directly in a builtin function add ll
Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
agar RTE aaye to range check karna container ki
container ki size ko variable me store kara kar use karna (nhi to RTE)
remember to put "\n" after every answer in case of multiple testcases
Dont keep array name as size or any other key word*/

// AH! HERE WE GO AGAIN :)
#include<bits/stdc++.h>
#include<string>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll              long long
#define ull             unsigned long long
#define ldb             long long double
#define pll             pair<long long, long long>
#define pb              push_back
#define pf              push_front
#define pof             pop_front
#define pob             pop_back
#define ft              front
#define sz              size
#define gcd             __gcd
#define bk              back
#define ub              upper_bound
#define lb              lower_bound
#define sum             accumulate
#define bs              binary_search
#define fr              first
#define sc              second
#define mp              make_pair
#define all(v)          v.begin(),v.end()
#define ins             insert
#define bg              begin
#define rep(i,a,b)      for(i=a;i<=b;i++)
#define repr(i,a,b)     for(i=a;i>=b;i--)
#define Ndigits         floor(log10(N)+1)
#define N               200001
#define M               1003
#define pi              3.141592653589793238
#define FASTIO          std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ordered_set     tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
    FASTIO
    //freopen("input.txt", "r", stdin);   // to be uncommented when I/P
    //freopen("output.txt", "w", stdout); // & O/P are in file format
    ll n,i,m,j,c=0,ans=0,d=1;
    cin>>n;
    m=(2*n);
    string s;
    vector<ll> v;
    while(m--)
    {
        cin>>s;
        if(s=="add")
        {
            cin>>j;
            v.pb(j);
        }
        else if(s=="remove")
        {
            if(v.back()!=d)
            {
                if(v.size()>c)
                {
                    ans++;
                    c=v.size();
                }
                v.pob();
            }
            d++;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <queue>
#include <string>

using std::cin;
using std::cout;
using std::priority_queue;
using std::string;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int                 end           = n << 1;
    int                 count         = 0;
    bool                sort_required = false;
    priority_queue<int> stack;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            if (!stack.empty() && box > stack.top()) sort_required = true;
            stack.push(box);
        } else {  // op == "remove"
            if (sort_required) {
                ++count;
                sort_required = false;
            }
            stack.pop();
        }
    }
    cout << count << '\n';
    return 0;
}


#include <iostream>
#include <stack>
#include <string>

using std::cin;
using std::cout;
using std::stack;
using std::string;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int        end   = n << 1;
    int        count = 0;
    int        cur   = 1;
    stack<int> box_stack;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            box_stack.push(box);
        } else {  // op == "remove"
            if (!box_stack.empty()) {
                if (box_stack.top() == cur) {
                    box_stack.pop();
                } else {
                    stack<int>().swap(box_stack);  // clear box_stack
                    ++count;
                }
            }
            ++cur;
        }
    }
    cout << count << '\n';
    return 0;
}


#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::priority_queue;
using std::string;
using std::vector;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int  end           = n << 1;
    int  count         = 0;
    bool sort_required = false;

    priority_queue<int, vector<int>, std::greater<int>> stack;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            // if (!stack.empty()) cout << stack.top() << '\n';
            if (!stack.empty() && box > stack.top()) sort_required = true;
            stack.push(box);
        } else {  // op == "remove"
            if (sort_required) {
                ++count;
                sort_required = false;
            }
            stack.pop();
        }
    }
    cout << count << '\n';
    return 0;
}


#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::priority_queue;
using std::sort;
using std::stack;
using std::string;
using std::vector;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int  end           = n << 1;
    int  count         = 0;
    bool sort_required = false;

    // priority_queue<int, vector<int>, std::greater<int>> stack;
    vector<int> stack;
    stack.reserve(n);
    int last = -1;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            // if (!stack.empty()) cout << stack.top() << '\n';
            // if (!sort_required && !stack.empty() && box > stack.top())
            //     sort_required = true;
            if (last != -1 && !sort_required && !stack.empty() && box > last)
                sort_required = true;
            stack.push_back(box);
            last = box;
        } else {  // op == "remove"
            if (sort_required) {
                ++count;
                sort(stack.begin(), stack.end(), std::greater<int>());
                sort_required = false;
                last          = stack.back();
            }
            if (!stack.empty()) stack.pop_back();
            if (stack.empty()) last = -1;
        }
    }
    cout << count << '\n';
    return 0;
}


#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::priority_queue;
using std::string;
using std::vector;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int  end           = n << 1;
    int  count         = 0;
    bool sort_required = false;

    priority_queue<int, vector<int>, std::greater<int>> stack;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            // if (!stack.empty()) cout << stack.top() << '\n';
            if (!sort_required && !stack.empty() && box > stack.top())
                sort_required = true;
            stack.push(box);
        } else {  // op == "remove"
            if (sort_required) {
                ++count;
                sort_required = false;
            }
            if (!stack.empty()) stack.pop();
        }
    }
    cout << count << '\n';
    return 0;
}


#include <iostream>
#include <stack>
#include <string>

using std::cin;
using std::cout;
using std::stack;
using std::string;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int        end   = n << 1;
    int        count = 0;
    int        cur   = 1;
    stack<int> box_stack;
    for (int i = 0; i < end; ++i) {
        string op;
        cin >> op;
        if (op[0] == 'a') {  // op == "add"
            int box;
            cin >> box;
            box_stack.push(box);
        } else {  // op == "remove"
            if (!box_stack.empty()) {
                if (box_stack.top() == cur) {
                    box_stack.pop();
                } else {
                    stack<int>().swap(box_stack);  // clear box_stack
                    ++count;
                }
                ++cur;
            }
        }
    }
    cout << count << '\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int n; cin>>n;
    int ss=2*n;
    int change=0;
    int cur=1;
    std::vector<int> v;
    priority_queue<int,std::vector<int>,greater<int>> q;
    while(ss--){
        string op; cin>>op;
        if(op=="add"){
            int x; cin>>x;
            q.push(x);
            v.push_back(x);
        }
        else{
            if(v.size()==0){
                q.pop();
                continue;
            }
            if(q.top()!=v.back()){
                change++;
                v.clear();
            }
            q.pop();
        }
    }
    cout<<change<<endl;
}
int32_t main(){
    // ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tt;
    tt=1;
    // cin>>tt;
    for(int t=1;t<=tt;t++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n; cin>>n;
    int ss=2*n;
    int cur=1;
    int change=0;
    priority_queue<int,std::vector<int>,greater<int>> q;
    q.push(1e9);
    while(ss--){
        string op; cin>>op;
        if(op=="add"){
            int x; cin>>x;
            if(x>q.top()){
                change++;
            }
            q.push(x);
        }
        else{
            q.pop();
        }
    }
    cout<<change<<endl;
}
int32_t main(){
    // ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tt;
    tt=1;
    // cin>>tt;
    for(int t=1;t<=tt;t++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n; cin>>n;
    int ss=2*n;
    std::vector<int> v;
    int cur=1;
    int change=0;
    while(ss--){
        string op; cin>>op;
        if(op=="add"){
            int x; cin>>x;
            v.push_back(x);
        }
        else{
            if(v.back()==cur){
                v.pop_back();
                cur++;
            }
            else{
                change++;
                cur++;
                sort(v.begin(),v.end());
                reverse(v.begin(),v.end());
                v.pop_back();
            }
        }
    }
    cout<<change<<endl;
}
int32_t main(){
    // ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tt;
    tt=1;
    // cin>>tt;
    for(int t=1;t<=tt;t++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int n; cin>>n;
    int ss=2*n;
    int change=0;
    int cur=1;
    std::vector<int> v;
    priority_queue<int,std::vector<int>,greater<int>> q;
    while(ss--){
        string op; cin>>op;
        if(op=="add"){
            int x; cin>>x;
            q.push(x);
            v.push_back(x);
        }
        else{
            if(v.size()==0){
                q.pop();
            }
            else{
                if(q.top()!=v.back()){
                    // cout<<q.top()<<" "<<v.back()<<endl;
                    change++;
                    v.clear();
                    q.pop();
                }
                else{
                    q.pop();
                    v.pop_back();
                }

            }
        }
    }
    cout<<change<<endl;
}
int32_t main(){
    // ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tt;
    tt=1;
    // cin>>tt;
    for(int t=1;t<=tt;t++){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n; cin>>n;
    int ss=2*n;
    int change=0;
    priority_queue<int,std::vector<int>,greater<int>> q;
    while(ss--){
        string op; cin>>op;
        if(op=="add"){
            int x; cin>>x;
            if(q.size()==0){
                q.push(x);
                continue;
            }
            if(x>q.top()){
                change++;
            }
            q.push(x);
        }
        else{
            q.pop();
        }
    }
    cout<<change<<endl;
}
int32_t main(){
    // ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tt;
    tt=1;
    // cin>>tt;
    for(int t=1;t<=tt;t++){
        solve();
    }
}


/*input
3
add 1
remove
add 2
add 3
remove
remove

*/
#include<bits/stdc++.h>
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			emplace_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define hell 		(ull)1e9
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;

int main(void)
{
	ios
	ll n;cin>>n;
	string s;ll x;
	ll c = 0;
	ll ans = 0;
	stack < ll > p;
	vi l;
	bool o=0;
	rep(i,0,2*n)
	{
		cin>>s;
		if (s == "add")
		{
			cin>>x;
			o=0;
			l.pb(x);
		}
		else
		{
			if (o || l[l.size()-1] == c+1)
			{
				c++;
			}
			else
			{
				o=1;
				ans++;
			}
		}
	}
	cout<<ans<<"\n";
}


/*input

7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

*/
#include<bits/stdc++.h>
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			emplace_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define hell 		(ull)1e9
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;

int main(void)
{
	ios
	ll n;cin>>n;
	string s;ll x;
	ll c = 0;
	ll ans = 0;
	stack < ll > p;
	rep(i,0,2*n)
	{
		cin>>s;
		if (s == "add")
		{
			cin>>x;
			p.push(x);
		}
		else
		{
			if (p.empty())
			{
				c++;
				continue;
			}
			if (p.top() == c+1)
			{
				p.pop();
			}
			else
			{
				while(!p.empty())
					p.pop();
				ans++;
			}
			c++;
		}
	}
	cout<<ans<<"\n";
}


/*input
3
add 1
remove
add 2
add 3
remove
remove

*/
#include<bits/stdc++.h>
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			emplace_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define hell 		(ull)1e9
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;

int main(void)
{
	ios
	ll n;cin>>n;
	string s;ll x;
	ll c = 0;
	ll ans = 0;
	stack < ll > p;
	vi l;
	bool o=0;
	rep(i,0,2*n)
	{
		cin>>s;
		if (s == "add")
		{
			cin>>x;
			o=0;
			l.pb(x);
		}
		else
		{
			if (o || l[l.size()-1] == c+1)
			{
				c++;
			}
			else
			{
				o=1;
				ans++;
				c++;
			}
		}
	}
	cout<<ans<<"\n";
}


/*input

7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

*/
#include<bits/stdc++.h>
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			emplace_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define hell 		(ull)1e9
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;

int main(void)
{
	ios
	ll n;cin>>n;
	string s;ll x;
	ll c = 0;
	ll ans = 0;
	stack < ll > p;
	rep(i,0,2*n)
	{
		cin>>s;
		if (s == "add")
		{
			cin>>x;
			p.push(x);
		}
		else
		{
			if (p.empty())
			{
				continue;
			}
			if (p.top() == c+1)
			{
				p.pop();
				c++;
			}
			else
			{
				while(!p.empty())
					p.pop();
				ans++;
			}
		}
	}
	cout<<ans<<"\n";
}


/*input

7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

*/
#include<bits/stdc++.h>
#define ull 		unsigned long long
#define ll 			long long
#define M 			1000000007
#define pb 			emplace_back
#define p_q 		priority_queue
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios	    	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp 			make_pair
#define lb 			lower_bound
#define ub			upper_bound
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a,n,b)	for(ll int i=0;i<n;i++) a[i]=0;
#define hell 		(ull)1e9
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;

int main(void)
{
	ios
	ll n;cin>>n;
	string s;ll x;
	ll c = 0;
	ll ans = 0;
	stack < ll > p;
	rep(i,0,2*n)
	{
		cin>>s;
		if (s == "add")
		{
			cin>>x;
			p.push(x);
		}
		else
		{
			if (p.empty())
			{
				continue;
			}
			if (p.top() == c+1)
			{
				p.pop();
			}
			else
			{
				while(!p.empty())
					p.pop();
				ans++;
			}
			c++;
		}
	}
	cout<<ans<<"\n";
}


// Okabe and Boxes
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int num[300010];

int main()
{
    int rsp = 0;
    int top = 1;
    int ans = 0;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < 2 * n; i++)
    {
        char a[10];
        scanf("%s", a);
        if (a[0] == 'a')
        {
            scanf("%d", &num[rsp++]);
        }
        else
        {
            if (rsp == 0)
                continue;
            else
            {
                if (num[rsp - 1] == top)
                    rsp--;
                else
                {
                    rsp = 0;
                    ans++;
                }
                top++;
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


// Okabe and Boxes
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int num[300010];

int main()
{
    int rsp = 0;
    int top = 1;
    int ans = 0;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < 2 * n; i++)
    {
        char a[10];
        scanf("%s", a);
        if (a[0] == 'a')
        {
            scanf("%d", &num[rsp++]);
        }
        else
        {
            if (rsp == 0)
                ;
            else
            {
                if (num[rsp - 1] == top)
                    rsp--;
                else
                {
                    rsp = 0;
                    ans++;
                }
            }
            top++;
        }
    }
    printf("%d\n", ans);

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
using namespace std;
bool num[300005];
int main()
{
    int n;
    scanf("%d",&n);
    stack<int> s;
    int cnt=1;
    int ans=0;
    for(int i=0;i<(n<<1);i++)
    {
        char c[10];
        scanf("%s",c);
        if(c[0]=='a')
        {
            int temp;
            scanf("%d",&temp);
            s.push(temp);
        }
        else
        {
            if(num[cnt]==true)
                cnt++;
            else if(s.top()==cnt)
            {
                cnt++;
                s.pop();
            }
            else
            {
                while(s.top()!=cnt)
                {
                    num[s.top()]=true;
                    s.pop();
                    ans++;
                }
                s.pop();
                cnt++;
            }
        }
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;

stack<int> stk;
int main()
{
    int n, ind = 1, res = 0, now = 1;
    scanf("%d", &n);
    for(int i = 0; i < 2*n; i++)
    {
        string s;
        int x;
        cin >> s;
        if(s == "add")
        {
            scanf("%d", &x);
            stk.push(x);
        }
        else
        {
            if(stk.empty())
            {
                continue;
            }
            else if(stk.top() != ind)
            {
                res++;
                while(!stk.empty()) stk.pop();
            }
            else
            {
                stk.pop();
            }
        }
    }
    printf("%d", res);
}