#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define F first
#define S second
#define make0(a) memset(a,0,sizeof(a))
#define make1(a) memset(a,-1,sizeof(a))
#define all(v) begin(v),end(v)
#define allr(v) v.rbegin(),v.rend()
#define pi pair <int,int>
#define pll pair <ll,ll>
#define cases int t; cin >> t; while(t--)
#define fast_cin() ios_base::sync_with_stdio(0)

const int mod = 1e9+7;
const int maxn = 1e5+10;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

int stree[maxn*4];

void makeTree(int i, int j, int sidx)
{
	if(i == j)
	{
		stree[sidx] = 0;
		return;
	}
	int mid = (i+j)/2;
	makeTree(i,mid,sidx*2);
	makeTree(mid+1,j,sidx*2+1);
	stree[sidx] = stree[sidx*2]+stree[sidx*2+1];
}

int l,r;
int idx,val;

int query(int i, int j, int sidx)
{
	// trace(i, j);
	if(r < l)
		return 0;
	if(i > r or j < l)
		return 0;
	if(i >= l and j <= r)
		return stree[sidx];
	int mid = (i+j)/2;
	return query(i,mid,sidx*2) + query(mid+1,j,sidx*2+1);
}

void update(int i, int j, int sidx)
{
	if(idx > j or idx < i)
		return;
	if(i == j)
	{
		stree[sidx] = val;
		return;
	}
	int mid = (i+j)/2;
	update(i,mid,sidx*2);
	update(mid+1,j,sidx*2+1);
	stree[sidx] = stree[sidx*2] + stree[sidx*2+1];
}

int glob = 1;

int dist(int a, int b)
{
	if(a >= b)
		return a-b;
	else
	{
		return (1000000-b+1) + a;
	}
}

bool CP(int a, int b)
{
	return dist(a, glob) < dist(b, glob);
}

int main()
{
	fast_cin();
	int N;
	cin >> N;
	pi A[N+1];
	int B[N+1];
	for(int i=1;i<=N;i++)
	{
		cin >> A[i].F;
		B[i] = A[i].F;
		A[i].S = i;
	}
	sort(A+1,A+N+1);
	// for(int i=1;i<=N;i++)
	// 	cout << A[i].S << endl;

	int P[N+1];
	for(int i=1;i<=N;i++)
		P[i] = A[i].S;
	// for(int i=1;i<=N;i++)
	// 	cerr << P[i] << " ";
	// cerr << endl;
	int ct = 0;
	int last = 1;
	ll ans = 0;
	// cerr << ans << endl;
	bool done[N+1];
	make0(done);
	for(int i=1;i<=N;i++)
	{
		if(done[B[P[i]]] == false)
		{
			// cerr << "HERE\n";
			int j;
			for(j=i;j<=N;j++)
			{
				if(j == N+1)
					break;
				if(B[P[i]] != B[P[j]])
					break;
				// cerr << P[j] << "	" << dist(P[j],last) << endl;
			}
			glob = last;
			sort(P+i, P+j, CP);
			done[B[P[i]]] = true;
		}
		// cerr << i << ' ' << j << endl;
		// for(int j=1;j<=N;j++)
		// 	cerr << P[j] << " ";
		// cerr << endl;
		int curr = P[i];
		if(curr >= last)
		{
			l = last;
			r = curr-1;
			int done = query(1, N, 1);
			// cerr << last << ' ' << curr << ' ' << done << ' ' << ans << endl;
			ans += (curr - last - done + 1)*1ll;
		}
		else
		{
			l = last;
			r = N;
			int done = query(1, N, 1);
			// trace(l, r, done);
			l = 1;
			r = curr-1;
			done += query(1, N, 1);
			int diff = N - last + 1;
			diff += curr;
			// cerr << last << ' ' << curr << ' ' << done << ' ' << ans << endl;
			ans += (diff - done)*1ll;
		}
		idx = curr;
		val = 1;
		last = curr + 1;
		// trace(idx);
		update(1, N, 1);
		if(last == N+1)
			last = 1;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int indt[300000],a[100010];
vector <int> v[100010];
#define IT_MAX 1<<17
void update(int p, int v) {
	p += IT_MAX - 1;
	indt[p] = v;
	for (p /= 2; p; p /= 2) indt[p] = indt[2 * p] + indt[2 * p + 1];
}
int getsum(int p1, int p2) {
	p1 += IT_MAX - 1;
	p2 += IT_MAX - 1;
	int rv = 0;
	for (; p1 <= p2; p1 /= 2, p2 /= 2) {
		if (p1 % 2 == 1) rv += indt[p1++];
		if (p2 % 2 == 0) rv += indt[p2--];
	}
	return rv;
}
int main(void) {
    int n;
    ll ans=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]].push_back(i);
        update(i,1);
    }
    int cur = 1;
    for(int i=1;i<=100003;i++)
    {
        if(v[i].size())
        {
            int j;
            for(j=0;j<v[i].size();j++)
            {
                if(v[i][j]>=cur)
                    break;
            }
            int cur_index = v[i].back();
            if(j) cur_index  = v[i][j-1];
            if(cur_index>=cur)
            ans+=getsum(cur,cur_index);
            else
            ans+=getsum(cur,n)+getsum(1,cur_index);
            cur = cur_index+1;
            if(cur>=n+1)
            cur=1;
            for(int k=0;k<v[i].size();k++)
            update(v[i][k],0);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int indt[300000],a[100010];
vector <int> v[100010];
#define IT_MAX 1<<17
void update(int p, int v) {
	p += IT_MAX - 1;
	indt[p] = v;
	for (p /= 2; p; p /= 2) indt[p] = indt[2 * p] + indt[2 * p + 1];
}
int getsum(int p1, int p2) {
	p1 += IT_MAX - 1;
	p2 += IT_MAX - 1;
	int rv = 0;
	for (; p1 <= p2; p1 /= 2, p2 /= 2) {
		if (p1 % 2 == 1) rv += indt[p1++];
		if (p2 % 2 == 0) rv += indt[p2--];
	}
	return rv;
}
int main(void) {
    int n,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]].push_back(i);
        update(i,1);
    }
    int cur = 1;
    for(int i=1;i<=100001;i++)
    {
        if(v[i].size())
        {
            int j;
            for(j=0;j<v[i].size();j++)
            {
                if(v[i][j]>=cur)
                    break;
            }
            int cur_index = v[i].back();
            if(j) cur_index  = v[i][j-1];
            if(cur_index>=cur)
            ans+=getsum(cur,cur_index);
            else
            ans+=getsum(cur,n)+getsum(1,cur_index);
            cur = cur_index+1;
            if(cur>=n+1)
            cur=1;
            for(int k=0;k<v[i].size();k++)
            update(v[i][k],0);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int indt[300000],a[100010];
vector <int> v[100010];
typedef long long int ll;
#define IT_MAX 1<<17
void update(int p, int v) {
	p += IT_MAX - 1;
	indt[p] = v;
	for (p /= 2; p; p /= 2) indt[p] = indt[2 * p] + indt[2 * p + 1];
}
int getsum(int p1, int p2) {
	p1 += IT_MAX - 1;
	p2 += IT_MAX - 1;
	int rv = 0;
	for (; p1 <= p2; p1 /= 2, p2 /= 2) {
		if (p1 % 2 == 1) rv += indt[p1++];
		if (p2 % 2 == 0) rv += indt[p2--];
	}
	return rv;
}
int main(void) {
    int n;
    ll ans=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]].push_back(i);
        update(i,1);
    }
    int cur = 1;
    for(int i=1;i<=100003;i++)
    {
        if(v[i].size())
        {
            int j;
            for(j=0;j<v[i].size();j++)
            {
                if(v[i][j]>=cur)
                    break;
            }
            int cur_index = v[i].back();
            if(j) cur_index  = v[i][j-1];
            if(cur_index>=cur)
            ans+=getsum(cur,cur_index);
            else
            ans+=getsum(cur,n)+getsum(1,cur_index);
            cur = cur_index+1;
            if(cur>=n+1)
            cur=1;
            for(int k=0;k<v[i].size();k++)
            update(v[i][k],0);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int indt[300000],a[100010];
vector <int> v[100010];
typedef long long int ll;
int IT_MAX = 1 << 17;
void update(int p, int v) {
	p += IT_MAX - 1;
	indt[p] = v;
	for (p /= 2; p; p /= 2) indt[p] = indt[2 * p] + indt[2 * p + 1];
}
int getsum(int p1, int p2) {
	p1 += IT_MAX - 1;
	p2 += IT_MAX - 1;
	int rv = 0;
	for (; p1 <= p2; p1 /= 2, p2 /= 2) {
		if (p1 % 2 == 1) rv += indt[p1++];
		if (p2 % 2 == 0) rv += indt[p2--];
	}
	return rv;
}
int main(void) {
    int n;
    ll ans=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]].push_back(i);
        update(i,1);
    }
    int cur = 1;
    for(int i=1;i<=100000;i++)
    {
        if(v[i].size())
        {
            int j=0;
            for(j=0;j<v[i].size();j++)
            {
                if(v[i][j]>=cur)
                    break;
            }
            int cur_index = v[i].back();
            if(j) cur_index  = v[i][j-1];
            if(cur_index>=cur)
            ans+=getsum(cur,cur_index);
            else
            ans+=getsum(cur,n)+getsum(1,cur_index);
            cur = cur_index+1;
            if(cur==n+1)
            cur=1;
            for(int k=0;k<v[i].size();k++)
            update(v[i][k],0);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(a) ((int)(a).size())
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int N=1e5;
vi a[N+5];ll ans;
int main(){
	int n,x;cin>>n;rep(i,1,n)R(x),a[x].pb(i);
	rep(i,1,N)sort(all(a[i]));
	int pre=0;rep(i,1,N)if(!a[i].empty()){
		int j=upper_bound(all(a[i]),pre)-a[i].begin();
		if(j==sz(a[i])){
			ans+=pre-a[i][0];
			ans+=*prev(a[i].end())-a[i][0];
			pre=*prev(a[i].end());
		}if(j==0){
			ans+=a[i][sz(a[i])-1]-pre;
			pre=a[i][sz(a[i])-1];
		}else{
			ans+=a[i][sz(a[i])-1]-pre;
			ans+=a[i][sz(a[i])-1]-a[i][0];
			ans+=a[i][j-1]-a[i][0];
			pre=a[i][j-1];
		}
	}
	cout<<ans;
}


#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(a) ((int)(a).size())
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int N=1e5;
vi a[N+5];ll ans,c[N];int n;
int get(int i){
	int r=0;for(;i>0;i-=i&-i)r+=c[i];
	return r;
}
int add(int i,int d){
	for(;i<=n;i+=i&-i)c[i]+=d;
}
int solve(int r,int l){
	return get(r)-get(l);
}
int main(){
	int x;cin>>n;rep(i,1,n)R(x),a[x].pb(i),add(i,1);
	rep(i,1,N)sort(all(a[i]));
	int pre=0;rep(i,1,N)if(!a[i].empty()){
		int j=upper_bound(all(a[i]),pre)-a[i].begin();
		if(j==sz(a[i])){
			ans+=solve(n,pre);
			ans+=solve(*prev(a[i].end()),0);
			pre=*prev(a[i].end());
		}else if(j==0){
			ans+=solve(a[i][sz(a[i])-1],pre);
			pre=a[i][sz(a[i])-1];
		}else{
			ans+=solve(a[i][sz(a[i])-1],pre);
			ans+=solve(n,a[i][sz(a[i])-1]);
			ans+=solve(a[i][j-1],0);
			pre=a[i][j-1];
		}
		for(int k:a[i])add(k,-1);
	}
	cout<<ans;
}


#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(a) ((int)(a).size())
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int N=1e5;
vi a[N+5];ll ans,c[N];int n;
int get(int i){
	int r=0;for(;i>0;i-=i&-i)r+=c[i];
	return r;
}
int add(int i,int d){
	for(;i<=n;i+=i&-i)c[i]+=d;
}
int solve(int r,int l){
	return get(r)-get(l);
}
int main(){
	int x;cin>>n;rep(i,1,n)R(x),a[x].pb(i),add(i,1);
	rep(i,1,N)sort(all(a[i]));
	int pre=0;rep(i,1,N)if(!a[i].empty()){
		int j=upper_bound(all(a[i]),pre)-a[i].begin();
		if(j==sz(a[i])){
			ans+=solve(n,pre);
			ans+=solve(*prev(a[i].end()),0);
			pre=*prev(a[i].end());
		}else if(j==0){
			ans+=solve(a[i][sz(a[i])-1],pre);
			pre=a[i][sz(a[i])-1];
		}else{
			ans+=solve(a[i][sz(a[i])-1],pre);
			ans+=solve(n,a[i][sz(a[i])-1])+solve(a[i][0],1);
			ans+=solve(a[i][j-1],a[i][0]);
			pre=a[i][j-1];
		}
		for(int k:a[i])add(k,-1);
	}
	cout<<ans;
}


#include<map>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(a) ((int)(a).size())
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define R(a) scanf("%d",&a)
#define per(i,a,b) for(int i=a;i>=int(b);i--)
#define rep(i,a,b) for(int i=a;i<=int(b);i++)
#define rix(i,x) for(int i=last[x];i;i=pre[i])
ll gcd(ll a,ll b){return a==0?b:gcd(b%a,a);}
ll pow(ll a,ll b,ll M) {ll t=1;for(a%=M;b;b>>=1){if(b&1)t=t*a%M;a=a*a%M;}return t;}
const int N=1e5;
vi a[N+5];ll ans,c[N+5];int n;
int get(int i){
	int r=0;for(;i>0;i-=i&-i)r+=c[i];
	return r;
}
int add(int i,int d){
	for(;i<=n;i+=i&-i)c[i]+=d;
}
int solve(int r,int l){
	return get(r)-get(l);
}
int main(){
	int x;cin>>n;rep(i,1,n)R(x),a[x].pb(i),add(i,1);
	int pre=0;rep(i,1,N)if(!a[i].empty()){
		int j=upper_bound(all(a[i]),pre)-a[i].begin();
		if(j==sz(a[i])){
			ans+=solve(n,pre);
			ans+=solve(*prev(a[i].end()),0);
			pre=*prev(a[i].end());
		}else if(j==0){
			ans+=solve(a[i][sz(a[i])-1],pre);
			pre=a[i][sz(a[i])-1];
		}else{
			ans+=solve(a[i][sz(a[i])-1],pre);
			ans+=solve(n,a[i][sz(a[i])-1]);
			ans+=solve(a[i][j-1],0);
			pre=a[i][j-1];
		}
		for(int k:a[i])add(k,-1);
	}
	cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define fast_io                     \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)

#define endl '\n'
const int N = 100001;
LL n;
int bit[N], vis[N];
void add(int idx, int u) {
  idx++;
  while (idx < N) {
    bit[idx] += u;
    idx += idx & -idx;
  }
}
int query(int p) {
  if (p < 0) return 0;
  p++;
  int ret = 0;
  while (p > 0) {
    ret += bit[p];
    p -= p & -p;
  }
  return ret;
}
int query(int a, int b) {
  if (b < a) return query(n) - query(a - 1) + query(b);
  return query(b) - query(a - 1);
}

int main() {
  fast_io;
  cin >> n;
  for (int i = 0; i < n; i++) add(i, 1);
  priority_queue<II> pq;
  for (LL i = 0; i < n; i++) {
    LL a;
    cin >> a;
    pq.push({-a, -i});
  }
  LL cl = 0;
  int cur = 0;
  while (!pq.empty()) {
    VI nexv;
    int tval = pq.top().F;
    while (!pq.empty() && pq.top().F == tval) nexv.PB(-pq.top().S), pq.pop();
    auto it = lower_bound(ALL(nexv), cur);
    auto jt = it;
    do {
      if (jt == nexv.end()) jt = nexv.begin();
      int nex = *jt;
      if (vis[nex]) break;
      vis[nex] = 1;
      LL miss = query(cur, nex);
      add(nex, -1);
      cl += miss;
      jt++;
      cur = next;
    } while (it != jt);
  }
  cout << cl << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define fast_io                     \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)

#define endl '\n'
const int N = 100001;
LL n;
int bit[N], vis[N];
void add(int n, int u) {
  while (n < N) {
    bit[n] += u;
    n += n & -n;
  }
}
int query(int p) {
  int ret = 0;
  while (p > 0) {
    ret += bit[p];
    p -= p & -p;
  }
  return ret;
}
int query(int a, int b) {
  if (b < a) return query(n) - query(a - 1) + query(b);
  return query(b) - query(a - 1);
}

int main() {
  fast_io;
  cin >> n;
  for (int i = 1; i <= n; i++) add(i, 1);
  priority_queue<II> pq;
  for (LL i = 1; i <= n; i++) {
    LL a;
    cin >> a;
    pq.push({-a, -i});
  }
  LL cl = 1;
  while (!pq.empty()) {
    VI nexv;
    int tval = pq.top().F;
    while (!pq.empty() && pq.top().F == tval) nexv.PB(-pq.top().S), pq.pop();
    auto it = lower_bound(ALL(nexv), cl % n == 0 ? n : (cl % n));
    auto jt = it;
    do {
      if (jt == nexv.end()) jt = nexv.begin();
      int nex = *jt;
      if (vis[nex]) break;
      vis[nex] = 1;
      LL miss = query(cl % n == 0 ? n : (cl % n), nex);
      add(nex, -1);
      cl += miss;
      jt++;
    } while (it != jt);
  }
  cout << cl - 1 << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define fast_io                     \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)

#define endl '\n'
const int N = 100001;
LL n;
int bit[N];
void add(int n, int u) {
  while (n < N) {
    bit[n] += u;
    n += n & -n;
  }
}
int query(int p) {
  int ret = 0;
  while (p > 0) {
    ret += bit[p];
    p -= p & -p;
  }
  return ret;
}
int query(int a, int b) {
  if (b < a) return query(n) - query(a - 1) + query(b);
  return query(b) - query(a - 1);
}

int main() {
  fast_io;
  cin >> n;
  for (int i = 1; i <= n; i++) add(i, 1);
  priority_queue<II> pq;
  for (LL i = 1; i <= n; i++) {
    LL a;
    cin >> a;
    pq.push({-a, -i});
  }
  LL cl = 1;
  while (!pq.empty()) {
    LL nex = -pq.top().S;
    pq.pop();
    LL miss = query(cl % n == 0 ? n : (cl % n), nex);
    // LL miss = query(cl, nex);
    add(nex, -1);
    cl += miss;
    // trace(miss, cl);
  }
  cout << cl - 1 << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define fast_io                     \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)

#define endl '\n'
const int N = 100001;
LL n;
int bit[N], vis[N];
void add(int idx, int u) {
  idx++;
  while (idx < N) {
    bit[idx] += u;
    idx += idx & -idx;
  }
}
int query(int p) {
  if (p < 0) return 0;
  p++;
  int ret = 0;
  while (p > 0) {
    ret += bit[p];
    p -= p & -p;
  }
  return ret;
}
int query(int a, int b) {
  if (b < a) return query(n) - query(a - 1) + query(b);
  return query(b) - query(a - 1);
}

int main() {
  fast_io;
  cin >> n;
  for (int i = 0; i < n; i++) add(i, 1);
  priority_queue<II> pq;
  for (LL i = 0; i < n; i++) {
    LL a;
    cin >> a;
    pq.push({-a, -i});
  }
  LL cl = 0;
  int cur = 0;
  while (!pq.empty()) {
    VI nexv;
    int tval = pq.top().F;
    while (!pq.empty() && pq.top().F == tval) nexv.PB(-pq.top().S), pq.pop();
    auto it = lower_bound(ALL(nexv), cur);
    auto jt = it;
    do {
      if (jt == nexv.end()) jt = nexv.begin();
      int nex = *jt;
      if (vis[nex]) break;
      vis[nex] = 1;
      LL miss = query(cur, nex);
      add(nex, -1);
      cl += miss;
      jt++;
      cur = nex;
    } while (it != jt);
  }
  cout << cl << endl;
  return 0;
}


#include <vector>
#include <stdio.h>
#include <algorithm>
int main(){

    unsigned n;
    unsigned s;
    unsigned counter = 0;
    scanf("%u",&n);
    std::vector<int> nums(n);
    std::vector<int> cache;
    for(int i=0;i<n;++i) {
        scanf("%u",&s);
        nums[i]=s;
    }
    cache.insert(cache.begin(),nums.begin(),nums.end());
    std::sort(cache.begin(),cache.end());
    int Cindex=0;
    int Nindex=0;
    auto num = cache[0];
    while (Cindex < n) {
        if(Nindex == n)
            Nindex = 0;
        auto tr = nums[Nindex];
        if (tr==num) {
            num = cache[++Cindex];
            nums[Nindex] = -1;
        }
        else if(tr==-1) {
            ++Nindex;
            continue;
        }
        ++counter;
        ++Nindex;
    }

    printf("%u",counter);

}


#include <deque>
#include <stdio.h>
#include <algorithm>
int main(){
    unsigned n;
    unsigned s;
    unsigned counter = 0;
    scanf("%u",&n);
    std::deque<int> nums(n);
    std::deque<int> cache;
    for(int i=0;i<n;++i) {
        scanf("%u",&s);
        nums[i]=s;
    }
    cache.insert(cache.begin(),nums.begin(),nums.end());
    std::sort(cache.begin(),cache.end());
    int Cindex=0;
    int Nindex=0;
    auto num = cache[0];
    while (Cindex < n) {
        if(Nindex == n)
            Nindex = 0;
        auto tr = nums[Nindex];
        if (tr==num) {
            num = cache[++Cindex];
            nums[Nindex] = -1;}
        else if(tr==-1) {
            ++Nindex;
            continue;
        }
        ++counter;
        ++Nindex;
    }
    printf("%u",counter);
return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>

using namespace std;
typedef long long ll;
int n,a[100001];
vector<int> vc[100001];
int main()
{
    scanf("%d",&n);
    int i,maxn=0;

    for (i=1;i<=n;i++)
        scanf("%d",&a[i]),maxn=max(maxn,a[i]);

    for (i=1;i<=n;i++)
        vc[a[i]].push_back(i);
    int pos=1;
    ll ans=0;
    int dis=1;
    for (i=1;i<=maxn;i++)
    {
        if (vc[i].empty()) continue;
        vector<int>::iterator iter;
        iter=lower_bound(vc[i].begin(),vc[i].end(),pos);
        ans+=1ll*dis*(vc[i].end()-iter);
        pos=*vc[i].rbegin();
        vc[i].erase(iter,vc[i].end());
        if (!vc[i].empty())
        {
            pos=1;
            dis++;
            i--;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


#include <vector>
#include <stdio.h>
#include <algorithm>
int main(){
    unsigned n;
    unsigned s;
    unsigned counter = 0;
    scanf("%u",&n);
    std::vector<int> nums(n);
    std::vector<int> cache;
    for(int i=0;i<n;++i) {
        scanf("%u",&s);
        nums[i]=s;
    }
    cache.insert(cache.begin(),nums.begin(),nums.end());
    std::sort(cache.begin(),cache.end());
    int Cindex=0;
    int Nindex=0;
    auto num = cache[0];
    while (Cindex < n) {
        if(Nindex == n)
            Nindex = 0;
        auto tr = nums[Nindex];
        if (tr==num) {
            num = cache[++Cindex];
            nums[Nindex] = -1;
        }
        else if(tr==-1) {
            ++Nindex;
            continue;
        }
        ++counter;
        ++Nindex;
    }
    printf("%u",counter);

}


// {{{
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ALL(x) begin(x),end(x)
#define SZ(x) ((int)x.size())
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define NOT_TO_SYNC ios::sync_with_stdio(false)
using namespace std;
using LL=long long;
using PII=pair<int,int>;
using VI=vector<int>;

#ifdef FEI
template<typename T>
void _dump(const char* s, T&& head) { cerr<<s<<"="<<head<<endl; }
template<typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
    int c=0;
    while (*s!=',' || c!=0) {
        if (*s=='(' || *s=='[' || *s=='{') c++;
        if (*s==')' || *s==']' || *s=='}') c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1, tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, ##__VA_ARGS__); \
} while(0);

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<'[';
    for (auto it=b; it!=e; it++) s<<(it==b?"":",")<<*it;
    s<<']';
    return s;
}

template<typename A, typename B>
ostream& operator <<(ostream &s, const pair<A,B> &p) { return s<<"("<<p.first<<","<<p.second<<")";}
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &x) { return _out(s,ALL(x)); }
template<typename T, size_t N>
ostream& operator <<(ostream &s, const array<T,N> &x) { return _out(s,ALL(x)); }
template<typename T>
ostream& operator <<(ostream &s, const set<T> &x) { return _out(s,ALL(x)); }
template<typename A, typename B>
ostream& operator <<(ostream &s, const map<A,B> &x) { return _out(s,ALL(x)); }
#else
#define dump(...)
#endif

template<typename T>
void _R(T &x) { cin>>x; }
void _R(int &x) { scanf("%d",&x); }
void _R(LL &x) { scanf("%" PRId64,&x); }
void _R(double &x) { scanf("%lf",&x); }
void _R(char &x) { scanf(" %c",&x); }
void _R(char *x) { scanf("%s",x); }

void R(){}
template<typename T, typename... X>
void R(T& head, X&... tail) { _R(head); R(tail...); }

const int MOD = 1e9+7;
// }}}

const int N = 100005;
set<int> st[N];
int arr[N], sorted[N], n;

int BIT[N];

int lowbit(int x) { return x & (-x); }

void add(int x, int v) {
    while (x <= n) {
        BIT[x] += v;
        x += lowbit(x);
    }
}

int query(int x) {
    int ret = 0;
    while (x) {
        ret += BIT[x];
        x -= lowbit(x);
    }
    return ret;
}

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

int main() {
    R(n);

    for (int i=1; i<=n; i++) {
        R(arr[i]);
        sorted[i] = arr[i];
        st[arr[i]].insert(i);
        add(i, 1);
    }

    sort(sorted+1, sorted+n+1);

    int pos = 0;
    LL ans = 0;

    for (int i=1; i<=n; i++) {
        set<int> &S = st[sorted[i]];
        auto it = S.lower_bound(pos);
        if (it == S.end()) it = S.begin();
        S.erase(it);
        int next = *it;

        if (pos < next) {
            ans += query(pos+1, next);
        }
        else {
            ans += query(pos+1, n);
            ans += query(1, next);
        }

        pos = next;
        add(next, -1);
    }

    cout << ans << endl;

    return 0;
}


// {{{
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ALL(x) begin(x),end(x)
#define SZ(x) ((int)x.size())
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define NOT_TO_SYNC ios::sync_with_stdio(false)
using namespace std;
using LL=long long;
using PII=pair<int,int>;
using VI=vector<int>;

#ifdef FEI
template<typename T>
void _dump(const char* s, T&& head) { cerr<<s<<"="<<head<<endl; }
template<typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
    int c=0;
    while (*s!=',' || c!=0) {
        if (*s=='(' || *s=='[' || *s=='{') c++;
        if (*s==')' || *s==']' || *s=='}') c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1, tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, ##__VA_ARGS__); \
} while(0);

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<'[';
    for (auto it=b; it!=e; it++) s<<(it==b?"":",")<<*it;
    s<<']';
    return s;
}

template<typename A, typename B>
ostream& operator <<(ostream &s, const pair<A,B> &p) { return s<<"("<<p.first<<","<<p.second<<")";}
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &x) { return _out(s,ALL(x)); }
template<typename T, size_t N>
ostream& operator <<(ostream &s, const array<T,N> &x) { return _out(s,ALL(x)); }
template<typename T>
ostream& operator <<(ostream &s, const set<T> &x) { return _out(s,ALL(x)); }
template<typename A, typename B>
ostream& operator <<(ostream &s, const map<A,B> &x) { return _out(s,ALL(x)); }
#else
#define dump(...)
#endif

template<typename T>
void _R(T &x) { cin>>x; }
void _R(int &x) { scanf("%d",&x); }
void _R(LL &x) { scanf("%" PRId64,&x); }
void _R(double &x) { scanf("%lf",&x); }
void _R(char &x) { scanf(" %c",&x); }
void _R(char *x) { scanf("%s",x); }

void R(){}
template<typename T, typename... X>
void R(T& head, X&... tail) { _R(head); R(tail...); }

const int MOD = 1e9+7;
// }}}

const int N = 100005;
set<int> st[N];
int arr[N], sorted[N], n;

int BIT[N];

int lowbit(int x) { return x & (-x); }

void add(int x, int v) {
    while (x <= n) {
        BIT[x] += v;
        x += lowbit(x);
    }
}

int query(int x) {
    int ret = 0;
    while (x) {
        ret += BIT[x];
        x -= lowbit(x);
    }
    return ret;
}

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

int main() {
    R(n);

    for (int i=1; i<=n; i++) {
        R(arr[i]);
        sorted[i] = arr[i];
        st[arr[i]].insert(i);
        add(i, 1);
    }

    sort(sorted+1, sorted+n+1);

    int pos = 0;
    LL ans = 0;

    for (int i=1; i<=n; i++) {
        set<int> &S = st[sorted[i]];
        auto it = S.lower_bound(pos);
        if (it == S.end()) it = S.begin();
        int next = *it;

        if (pos < next) {
            ans += query(pos+1, next);
        }
        else {
            ans += query(pos+1, n);
            ans += query(1, next);
        }

        pos = next;
        add(next, -1);
        S.erase(it);
    }

    cout << ans << endl;

    return 0;
}


// {{{
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ALL(x) begin(x),end(x)
#define SZ(x) ((int)x.size())
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define NOT_TO_SYNC ios::sync_with_stdio(false)
using namespace std;
using LL=long long;
using PII=pair<int,int>;
using VI=vector<int>;

#ifdef FEI
template<typename T>
void _dump(const char* s, T&& head) { cerr<<s<<"="<<head<<endl; }
template<typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
    int c=0;
    while (*s!=',' || c!=0) {
        if (*s=='(' || *s=='[' || *s=='{') c++;
        if (*s==')' || *s==']' || *s=='}') c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1, tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, ##__VA_ARGS__); \
} while(0);

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<'[';
    for (auto it=b; it!=e; it++) s<<(it==b?"":",")<<*it;
    s<<']';
    return s;
}

template<typename A, typename B>
ostream& operator <<(ostream &s, const pair<A,B> &p) { return s<<"("<<p.first<<","<<p.second<<")";}
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &x) { return _out(s,ALL(x)); }
template<typename T, size_t N>
ostream& operator <<(ostream &s, const array<T,N> &x) { return _out(s,ALL(x)); }
template<typename T>
ostream& operator <<(ostream &s, const set<T> &x) { return _out(s,ALL(x)); }
template<typename A, typename B>
ostream& operator <<(ostream &s, const map<A,B> &x) { return _out(s,ALL(x)); }
#else
#define dump(...)
#endif

template<typename T>
void _R(T &x) { cin>>x; }
void _R(int &x) { scanf("%d",&x); }
void _R(LL &x) { scanf("%" PRId64,&x); }
void _R(double &x) { scanf("%lf",&x); }
void _R(char &x) { scanf(" %c",&x); }
void _R(char *x) { scanf("%s",x); }

void R(){}
template<typename T, typename... X>
void R(T& head, X&... tail) { _R(head); R(tail...); }

const int MOD = 1e9+7;
// }}}

const int N = 100005;
set<int> st[N];
int arr[N], sorted[N], n;

int BIT[N];

int lowbit(int x) { return x & (-x); }

void add(int x, int v) {
    while (x <= n) {
        BIT[x] += v;
        x += lowbit(x);
    }
}

int query(int x) {
    int ret = 0;
    while (x) {
        ret += BIT[x];
        x -= lowbit(x);
    }
    return ret;
}

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

int main() {
    R(n);

    for (int i=1; i<=n; i++) {
        R(arr[i]);
        sorted[i] = arr[i];
        st[arr[i]].insert(i);
        add(i, 1);
    }

    sort(sorted+1, sorted+n+1);

    int pos = 0, ans = 0;

    for (int i=1; i<=n; i++) {
        set<int> &S = st[sorted[i]];
        auto it = lower_bound(S.begin(), S.end(), pos);
        if (it == S.end()) it = S.begin();

        dump(pos, *it);
        if (pos < *it) {
            ans += query(pos+1, *it);
        }
        else {
            ans += query(pos+1, n);
            ans += query(1, *it);
        }
        dump(ans);

        pos = *it;
        add(*it, -1);
        S.erase(it);
    }

    cout << ans << endl;

    return 0;
}


// {{{
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ALL(x) begin(x),end(x)
#define SZ(x) ((int)x.size())
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define NOT_TO_SYNC ios::sync_with_stdio(false)
using namespace std;
using LL=long long;
using PII=pair<int,int>;
using VI=vector<int>;

#ifdef FEI
template<typename T>
void _dump(const char* s, T&& head) { cerr<<s<<"="<<head<<endl; }
template<typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
    int c=0;
    while (*s!=',' || c!=0) {
        if (*s=='(' || *s=='[' || *s=='{') c++;
        if (*s==')' || *s==']' || *s=='}') c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1, tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, ##__VA_ARGS__); \
} while(0);

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<'[';
    for (auto it=b; it!=e; it++) s<<(it==b?"":",")<<*it;
    s<<']';
    return s;
}

template<typename A, typename B>
ostream& operator <<(ostream &s, const pair<A,B> &p) { return s<<"("<<p.first<<","<<p.second<<")";}
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &x) { return _out(s,ALL(x)); }
template<typename T, size_t N>
ostream& operator <<(ostream &s, const array<T,N> &x) { return _out(s,ALL(x)); }
template<typename T>
ostream& operator <<(ostream &s, const set<T> &x) { return _out(s,ALL(x)); }
template<typename A, typename B>
ostream& operator <<(ostream &s, const map<A,B> &x) { return _out(s,ALL(x)); }
#else
#define dump(...)
#endif

template<typename T>
void _R(T &x) { cin>>x; }
void _R(int &x) { scanf("%d",&x); }
void _R(LL &x) { scanf("%" PRId64,&x); }
void _R(double &x) { scanf("%lf",&x); }
void _R(char &x) { scanf(" %c",&x); }
void _R(char *x) { scanf("%s",x); }

void R(){}
template<typename T, typename... X>
void R(T& head, X&... tail) { _R(head); R(tail...); }

const int MOD = 1e9+7;
// }}}

const int N = 100005;
set<int> st[N];
int arr[N], sorted[N], n;

int BIT[N];

int lowbit(int x) { return x & (-x); }

void add(int x, int v) {
    while (x <= n) {
        BIT[x] += v;
        x += lowbit(x);
    }
}

int query(int x) {
    int ret = 0;
    while (x) {
        ret += BIT[x];
        x -= lowbit(x);
    }
    return ret;
}

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

int main() {
    R(n);

    for (int i=1; i<=n; i++) {
        R(arr[i]);
        sorted[i] = arr[i];
        st[arr[i]].insert(i);
        add(i, 1);
    }

    sort(sorted+1, sorted+n+1);

    int pos = 0;
    LL ans = 0;

    for (int i=1; i<=n; i++) {
        set<int> &S = st[sorted[i]];
        auto it = lower_bound(S.begin(), S.end(), pos);
        if (it == S.end()) it = S.begin();

        if (pos < *it) {
            ans += query(pos+1, *it);
        }
        else {
            ans += query(pos+1, n);
            ans += query(1, *it);
        }

        pos = *it;
        add(*it, -1);
        S.erase(it);
    }

    cout << ans << endl;

    return 0;
}


// Infinity on Jul 15, 2017

#include <bits/stdc++.h>

using namespace std;

inline namespace Infinity
{
#define define_pair(classname, x, y) struct classname { int x; int y; \
    classname(int x = 0, int y = 0) : x(x), y(y) {} };
#define define_triple(classname, x, y, z) struct classname { int x; int y; int z; \
    classname(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} };
#define less_than(classname) bool operator <(ConstRef<classname> lhs, ConstRef<classname> rhs)
#define bool_lambda(type) [](const type &lhs, const type &rhs)

inline namespace Constant
{
constexpr const int eof         = EOF;
constexpr const int maxint      = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
constexpr const double eps      = DBL_EPSILON;
} // namespace Infinity::Constant

inline namespace TypeDefine
{
using uint  = unsigned int;
using ll    = long long int;
using ull   = unsigned long long int;
using ld    = long double;
template<typename T> using ConstRef = const T &;
template<typename T = int> using Pair = pair<T, T>;
template<typename T = int> using PairList = vector<Pair<T>>;
template<typename T = vector<int>> using Iter = typename T::iterator;
} // namespace Infinity::TypeDefine

inline namespace Miscelleneous
{
inline constexpr int ctoi(const char c)
{ return c - '0'; }

inline constexpr char itoc(const int n)
{ return n + '0'; }

inline int dtoi(const double d)
{ return round(d); }

template<typename T> inline constexpr bool in(T x, T l, T r)
{ return l <= x && x <= r; }

template<typename T> inline constexpr T tristate(int val, T nag, T zero, T pos)
{ return val > 0 ? pos : val < 0 ? nag : zero; }

template<class T> inline int size(const T &a)
{ return a.size(); }

template<class T> inline void sort(T &a)
{ sort(a.begin(), a.end()); }

template<class T1, class T2> inline void sort(T1 &a, T2 comp)
{ sort(a.begin(), a.end(), comp); }

template<class T1, typename T2> inline int lbound(const T1 &a, const T2 k)
{ return lower_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, typename T2> inline int ubound(const T1 &a, const T2 k)
{ return upper_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, class T2> inline int count(T1 &a, T2 k)
{ return ubound(a, k) - lbound(a, k); }

template<class T> inline void unique_erase(T &a)
{ a.erase(unique(a.begin(), a.end()), a.end()); }

template<typename T> inline void clear(T &a)
{ memset(a, 0, sizeof a); }

template<typename T> T gcd(T a, T b)
{ while (b) { T t = a % b; a = b; b = t; } return a; }

template<typename T> T lcm(T a, T b)
{ return a / gcd(a, b) * b; }

long long qpow(long long a, long long b, long long c)
{ return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; }

// find (x, y) s.t. a x + b y = gcd(a, b) = d
template<typename T> T exGcd(T a, T b, T &x, T &y)
{ T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; }

// returns number of nk's in [l, r]
template<typename T> inline constexpr T mps(T l, T r, T k)
{ return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; }

template<typename T> inline constexpr T sgn(T a)
{ return a ? a > 0 ? 1 : -1 : 0; }

template<typename T> inline constexpr T sq(T a)
{ return a * a; }

template<typename T> inline constexpr bool odd(T a)
{ return bool(a % 2); }

template<typename T> inline constexpr bool even(T a)
{ return !bool(a % 2); }

// simple mod
template<typename T1, typename T2> inline constexpr T1 smod(T1 x, T2 m)
{ return x > m ? x - m : x + m < m ? x + m : x; }

// lengthes of recurring period of replacement `a'
vector<int> repetend(vector<int> a)
{ vector<int> p(a.size(), -1);
  for (unsigned i = 0; i < a.size(); i++) if (p[i] == -1) {
    int c = 1; for (unsigned j = a[i]; j != i; j = a[j]) ++c;
    p[i] = c; for (unsigned j = a[i]; j != i; j = a[j]) p[j] = c;
  } return p; }

template<typename T> inline constexpr T lmiddle(T first, T last)
{ return first + (last - first) / 2; }

template<typename T> inline constexpr T rmiddle(T first, T last)
{ return last - (last - first) / 2; }

// returns the largest value in [l, r) s.t. check(x)
template<typename T, typename F> T dichotomy(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = lmiddle(l, r); (check(m) ? l : r) = m; } return l; }

// returns the smallest value in (l, r] s.t. check(x)
template<typename T, typename F> T dichotomy2(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = rmiddle(l, r); (check(m) ? r : l) = m; } return r; }
} // namespace Infinity::Miscelleneous

inline namespace BattleLab
{
template<typename T>
vector<int> argsort(const vector<T> &a)
{ vector<pair<T, int>> b; for (unsigned i = 0; i < a.size(); i++) { b.emplace_back(a[i], i); }
  stable_sort(b.begin(), b.end()); vector<int> c; for (pair<T, int> p : b) { c.push_back(p.second); }
  return c; }

template<typename T>
vector<T> rearrange(const vector<T> &a, const vector<int>& b)
{ vector<T> c; for (int i : b) { c.push_back(a[i]); } return c; }

// Array begins

struct
{
    int i, n;
    bool first() const { return i == 0; }
    bool last() const { return i == n - 1; }
} loop;

#if __cplusplus >= 201402L
#define STL_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return std::func(this->begin(), this->end(), args...); }
#define INFINITY_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return ::func(*this, args...); }
#define INFINITY_ALGO_OO_OVERRIDE(func, return_type) \
    template<typename... types> return_type func(types ...args) \
    { return ::func(*this, args...); }
#define PAST_PARTICIPLE(stem, pp) \
    template<typename... types> Array<T> &pp(types ...args) \
    { stem(args...); return *this; }
#endif

template<typename T>
class Array : public vector<T>
{
public:
    using vector<T>::vector;

    Array() = default;

    Array(const vector<T> &a) : vector<T>(a)
    {}

    int length() const
    { return this->size(); }

    template<typename... types> Array<T> &append(types ...args)
    { emplace_back(args...); return *this; }

    T sum() const
    { T s = T(); for (T val : *this) { s += val; } return s; }

    Array<T> &add(T increment = 1)
    { for (T &val : *this) { val += increment; } return *this; }

    Array<T> &unique()
    { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; }

    template<typename F> void foreach(F func)
    { loop.n = length(); for (loop.i = 0; loop.i < loop.n; loop.i++) { func(this->at(loop.i)); } }

    template<typename F> Array<pair<int, int>> segments(F func)
    { Array<pair<int, int>> a; int i = 0; while (i < length()) { int j = i + 1;
      while (j < length() && func(this->at(j - 1), this->at(j))) { j++; } a.emplace_back(i, j); i = j; } return a; }

#if defined(STL_ALGO_OO) && defined(PAST_PARTICIPLE)
    STL_ALGO_OO(sort)           PAST_PARTICIPLE(sort, sorted)
    STL_ALGO_OO(stable_sort)    PAST_PARTICIPLE(stable_sort, stable_sorted)
    STL_ALGO_OO(reverse)        PAST_PARTICIPLE(reverse, reversed)
    STL_ALGO_OO(lower_bound)    STL_ALGO_OO(next_permutation)
    STL_ALGO_OO(upper_bound)    STL_ALGO_OO(prev_permutation)
    STL_ALGO_OO(count)          STL_ALGO_OO(count_if)
    STL_ALGO_OO(find)           STL_ALGO_OO(find_if)
    STL_ALGO_OO(fill)           /*STL_ALGO_OO(for_each)*/
    STL_ALGO_OO(iota)           STL_ALGO_OO(partial_sum)
#endif
#if defined(INFINITY_ALGO_OO) && defined(INFINITY_ALGO_OO)
    INFINITY_ALGO_OO(lbound)    INFINITY_ALGO_OO(ubound)
    INFINITY_ALGO_OO_OVERRIDE(argsort, Array<int>)
#endif
};

Array<int> getints(unsigned size)
{ Array<int> a(size); for (int &x : a) { scanf("%d", &x); } return a; }

using ints = Array<int>;
using doubles = Array<double>;
using llongs = Array<long long>;

#undef STL_ALGO_OO
#undef INFINITY_ALGO_OO
#undef INFINITY_ALGO_OO_OVERRIDE
#undef PAST_PARTICIPLE

// Array ends

class Division
{
public:
    Division(int n, int k) : lb(n / k), ub(lb + 1), b(n % k)
    {}

    int operator[](int i)
    { return i < b ? ub : lb; }

protected:
    int lb, ub, b;
};

template<typename T>
class AdjacencyList : public vector<vector<T>>
{
public:
    AdjacencyList(unsigned size = 0) : vector<vector<T>>(size)
    {}

    void addDirected(T u, T v)
    { (*this)[u].push_back(v); }

    void addUndirected(T u, T v)
    { addDirected(u, v); addDirected(v, u); }
};

class Range
{
    class RangeIterator
    {
    public:
        RangeIterator(int current = 0, int step = 1) : current(current), step(step) {}
        // works fine with c++11 range-based for loop ... only
        bool operator !=(const RangeIterator &iterator) const { return current < iterator.current; }
        int operator *() const { return current; }
        RangeIterator operator ++() { return {current += step, step}; }
    protected:
        int current, step;
    };
public:
    Range(int to, int from = 0, int step = 1) : from(from), to(to), step(step) {}
    RangeIterator begin() const { return {from, step}; }
    RangeIterator end() const { return {to, step}; }
protected:
    int from, to, step;
};

typedef Range range;
} // namespace Infinity::BattleLab

inline namespace IO
{
const char LF = '\n';
const char SP = ' ';

int dbl_prec = 10;
int ldbl_prec = 18;

inline void print(const int n)
{ printf("%d", n); }

inline void print(const unsigned n)
{ printf("%u", n); }

//#ifndef __i386__
inline void print(const long long n)
{ printf("%lld", n); }

inline void print(const unsigned long long n)
{ printf("%lld", n); }
//#endif

//inline void print(const int64_t n)
//{ printf("%" PRId64, n); }

//inline void print(const uint64_t n)
//{ printf("%" PRIu64, n); }

inline void print(const double a)
{ printf("%.*f", dbl_prec, a); }

inline void print(const long double a)
{ printf("%.*Lf", ldbl_prec, a); }

inline void print(const char c)
{ printf("%c", c); }

inline void print(const char s[])
{ printf("%s", s); }

inline void print(const string &s)
{ printf("%s", s.data()); }

template<typename T> inline void printSP(const T a)
{ print(a); print(SP); }

template<typename T, typename U> inline void print(const pair<T, U> &p)
{ printSP(p.first); print(p.second); }

template<class T> inline void print(const T a)
{ for (auto i = a.begin(); i != a.end(); i++) { if (i != a.begin()) print(SP); print(*i); } }

template<typename T> inline void print(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } }

template<typename T> inline void printr(T begin, T end)
{ for (print(*begin++); begin != end; ++begin) print(SP), print(*begin); }

inline void println()
{ print(LF); }

inline void printSP()
{ print(SP); }

template<typename T> inline void println(const T &a)
{ print(a); print(LF); }

template<typename T> inline void println(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } print(LF); }

template<class T> inline void printlns(const T &a)
{ for (auto n : a) println(n); }

template<typename T, typename... types> inline void print(const T &a, const types &...args)
{ print(a); print(args...); }

template<typename... types> inline void println(const types &...args)
{ print(args...); print(LF); }

template<typename... types> inline void printSP(const types &...args)
{ print(args...); print(SP); }

inline void printlnYN(bool b)
{ println(b ? "YES" : "NO"); }

inline void printlnyn(bool b)
{ println(b ? "Yes" : "No"); }

string caseN(int n, bool sharp = true, bool space = true)
{ return string("Case ") + (sharp ? "#" : "") + to_string(n) + ":" + (space ? " " : ""); }

inline int read(int &n)
{ return scanf("%d", &n); }

inline int read(int64_t &n)
{ return scanf("%" SCNd64, &n); }

template<typename T, typename ... types> inline int read(T &n, types &...args)
{ return read(n) == eof ? eof : read(args...) + 1; }

inline char getcc()
{ char c; do c = getchar(); while (isspace(c)); return c; }

inline int getint()
{ int n; read(n); return n; }

inline int64_t getll()
{ int64_t n; read(n); return n; }

inline double getdbl()
{ double n; scanf("%lf", &n); return n; }

inline pair<int, int> getpair()
{ pair<int, int> p; scanf("%d%d", &p.first, &p.second); return p; }

//inline vector<int> getints(int n)
//{ vector<int> v(n); for (int &i : v) i = getint(); return v; }

inline vector<pair<int, int> > getpairs(int n)
{ vector<pair<int, int> > v(n); for (pair<int, int> &p : v) { p = getpair(); } return v; }

inline void read(string &str, unsigned size)
{ char s[++size]; scanf("%s", s); str.assign(s); }

inline string getstr(unsigned size = 0x100000)
{ string s; read(s, size); return s; }

inline string getln(unsigned size = 0x100000)
{ char s[++size]; scanf("%[^\n]", s); getchar(); return s; }
} // namespace Infinity::IO
} // namespace Infinity

class BinaryIndexedTree
{
public:
    BinaryIndexedTree(int n) : a(n + 1) {}
    int sum(unsigned n) const { int s = 0; for (; n > 0; n -= -n & n) s += a[n]; return s; }
    int sum(unsigned l, unsigned r) const { return sum(r) - sum(l - 1); }
    void add(unsigned p, const int k) { if (p) for (; p < a.size(); p += -p & p) a[p] += k; }
protected:
    vector<int> a;
};

int main() /* Codeforces 830B */
{
    int n = getint();
    ints a = getints(n);
    ints b = a.argsort().add();
    b.segments([&](int i, int j){
        return a[i - 1] == a[j - 1];
    }).foreach([&](auto p){
        if (loop.first() == false) {
            rotate(&b[p.first], upper_bound(&b[p.first], &b[p.second], b[p.first - 1]), &b[p.second]);
        }
    });
    BinaryIndexedTree bit(n);
    for (int i = 1; i <= n; i++) {
        bit.add(i, 1);
    }
    int c = b[0];
    long long s = c;
    bit.add(b[0], -1);
    for (int i = 1; i < n; i++) {
        if (c < b[i]) {
            s += bit.sum(c, b[i]);
        } else {
            s += bit.sum(c, n) + bit.sum(b[i]);
        }
        c = b[i];
        bit.add(b[i], -1);
    }
    println(s);
    return 0;
}


// Infinity on Jul 15, 2017

#include <bits/stdc++.h>

using namespace std;

inline namespace Infinity
{
#define define_pair(classname, x, y) struct classname { int x; int y; \
    classname(int x = 0, int y = 0) : x(x), y(y) {} };
#define define_triple(classname, x, y, z) struct classname { int x; int y; int z; \
    classname(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} };
#define less_than(classname) bool operator <(ConstRef<classname> lhs, ConstRef<classname> rhs)
#define bool_lambda(type) [](const type &lhs, const type &rhs)

inline namespace Constant
{
constexpr const int eof         = EOF;
constexpr const int maxint      = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
constexpr const double eps      = DBL_EPSILON;
} // namespace Infinity::Constant

inline namespace TypeDefine
{
using uint  = unsigned int;
using ll    = long long int;
using ull   = unsigned long long int;
using ld    = long double;
template<typename T> using ConstRef = const T &;
template<typename T = int> using Pair = pair<T, T>;
template<typename T = int> using PairList = vector<Pair<T>>;
template<typename T = vector<int>> using Iter = typename T::iterator;
} // namespace Infinity::TypeDefine

inline namespace Miscelleneous
{
inline constexpr int ctoi(const char c)
{ return c - '0'; }

inline constexpr char itoc(const int n)
{ return n + '0'; }

inline int dtoi(const double d)
{ return round(d); }

template<typename T> inline constexpr bool in(T x, T l, T r)
{ return l <= x && x <= r; }

template<typename T> inline constexpr T tristate(int val, T nag, T zero, T pos)
{ return val > 0 ? pos : val < 0 ? nag : zero; }

template<class T> inline int size(const T &a)
{ return a.size(); }

template<class T> inline void sort(T &a)
{ sort(a.begin(), a.end()); }

template<class T1, class T2> inline void sort(T1 &a, T2 comp)
{ sort(a.begin(), a.end(), comp); }

template<class T1, typename T2> inline int lbound(const T1 &a, const T2 k)
{ return lower_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, typename T2> inline int ubound(const T1 &a, const T2 k)
{ return upper_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, class T2> inline int count(T1 &a, T2 k)
{ return ubound(a, k) - lbound(a, k); }

template<class T> inline void unique_erase(T &a)
{ a.erase(unique(a.begin(), a.end()), a.end()); }

template<typename T> inline void clear(T &a)
{ memset(a, 0, sizeof a); }

template<typename T> T gcd(T a, T b)
{ while (b) { T t = a % b; a = b; b = t; } return a; }

template<typename T> T lcm(T a, T b)
{ return a / gcd(a, b) * b; }

long long qpow(long long a, long long b, long long c)
{ return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; }

// find (x, y) s.t. a x + b y = gcd(a, b) = d
template<typename T> T exGcd(T a, T b, T &x, T &y)
{ T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; }

// returns number of nk's in [l, r]
template<typename T> inline constexpr T mps(T l, T r, T k)
{ return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; }

template<typename T> inline constexpr T sgn(T a)
{ return a ? a > 0 ? 1 : -1 : 0; }

template<typename T> inline constexpr T sq(T a)
{ return a * a; }

template<typename T> inline constexpr bool odd(T a)
{ return bool(a % 2); }

template<typename T> inline constexpr bool even(T a)
{ return !bool(a % 2); }

// simple mod
template<typename T1, typename T2> inline constexpr T1 smod(T1 x, T2 m)
{ return x > m ? x - m : x + m < m ? x + m : x; }

// lengthes of recurring period of replacement `a'
vector<int> repetend(vector<int> a)
{ vector<int> p(a.size(), -1);
  for (unsigned i = 0; i < a.size(); i++) if (p[i] == -1) {
    int c = 1; for (unsigned j = a[i]; j != i; j = a[j]) ++c;
    p[i] = c; for (unsigned j = a[i]; j != i; j = a[j]) p[j] = c;
  } return p; }

template<typename T> inline constexpr T lmiddle(T first, T last)
{ return first + (last - first) / 2; }

template<typename T> inline constexpr T rmiddle(T first, T last)
{ return last - (last - first) / 2; }

// returns the largest value in [l, r) s.t. check(x)
template<typename T, typename F> T dichotomy(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = lmiddle(l, r); (check(m) ? l : r) = m; } return l; }

// returns the smallest value in (l, r] s.t. check(x)
template<typename T, typename F> T dichotomy2(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = rmiddle(l, r); (check(m) ? r : l) = m; } return r; }
} // namespace Infinity::Miscelleneous

inline namespace BattleLab
{
template<typename T>
vector<int> argsort(const vector<T> &a)
{ vector<pair<T, int>> b; for (unsigned i = 0; i < a.size(); i++) { b.emplace_back(a[i], i); }
  stable_sort(b.begin(), b.end()); vector<int> c; for (pair<T, int> p : b) { c.push_back(p.second); }
  return c; }

template<typename T>
vector<T> rearrange(const vector<T> &a, const vector<int>& b)
{ vector<T> c; for (int i : b) { c.push_back(a[i]); } return c; }

// Array begins

struct
{
    int i, n;
    bool first() const { return i == 0; }
    bool last() const { return i == n - 1; }
} loop;

#define STL_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return std::func(this->begin(), this->end(), args...); }

#define INFINITY_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return ::func(*this, args...); }

#define INFINITY_ALGO_OO_OVERRIDE(func, return_type) \
    template<typename... types> return_type func(types ...args) \
    { return ::func(*this, args...); }

#define PAST_PARTICIPLE(stem, pp) \
    template<typename... types> Array<T> &pp(types ...args) \
    { stem(args...); return *this; }

template<typename T>
class Array : public vector<T>
{
public:
    using vector<T>::vector;

    Array() = default;

    Array(const vector<T> &a) : vector<T>(a)
    {}

    int length() const
    { return this->size(); }

    template<typename... types>
    Array<T> &append(types ...args)
    { emplace_back(args...); return *this; }

    T sum() const
    { T s = T(); for (T val : *this) { s += val; } return s; }

    Array<T> &add(T increment = 1)
    { for (T &val : *this) { val += increment; } return *this; }

    Array<T> &unique()
    { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; }

    template<typename F> void foreach(F func)
    { loop.n = length(); for (loop.i = 0; loop.i < loop.n; loop.i++) { func(this->at(loop.i)); } }

    template<typename F> Array<pair<int, int>> segments(F func)
    { Array<pair<int, int>> a; int i = 0; while (i < length()) { int j = i + 1;
      while (j < length() && func(this->at(j - 1), this->at(j))) { j++; } a.emplace_back(i, j); i = j; } return a; }

    STL_ALGO_OO(sort)           PAST_PARTICIPLE(sort, sorted)
    STL_ALGO_OO(stable_sort)    PAST_PARTICIPLE(stable_sort, stable_sorted)
    STL_ALGO_OO(reverse)        PAST_PARTICIPLE(reverse, reversed)
    STL_ALGO_OO(lower_bound)    STL_ALGO_OO(next_permutation)
    STL_ALGO_OO(upper_bound)    STL_ALGO_OO(prev_permutation)
    STL_ALGO_OO(count)          STL_ALGO_OO(count_if)
    STL_ALGO_OO(find)           STL_ALGO_OO(find_if)
    STL_ALGO_OO(fill)           /*STL_ALGO_OO(for_each)*/
    STL_ALGO_OO(iota)           STL_ALGO_OO(partial_sum)
    INFINITY_ALGO_OO(lbound)    INFINITY_ALGO_OO(ubound)
    INFINITY_ALGO_OO_OVERRIDE(argsort, Array<int>)
};

Array<int> getints(unsigned size)
{ Array<int> a(size); for (int &x : a) { scanf("%d", &x); } return a; }

using ints = Array<int>;
using doubles = Array<double>;
using llongs = Array<long long>;

#undef STL_ALGO_OO
#undef INFINITY_ALGO_OO
#undef INFINITY_ALGO_OO_OVERRIDE
#undef PAST_PARTICIPLE

// Array ends

class Division
{
public:
    Division(int n, int k) : lb(n / k), ub(lb + 1), b(n % k)
    {}

    int operator[](int i)
    { return i < b ? ub : lb; }

protected:
    int lb, ub, b;
};

template<typename T>
class AdjacencyList : public vector<vector<T>>
{
public:
    AdjacencyList(unsigned size = 0) : vector<vector<T>>(size)
    {}

    void addDirected(T u, T v)
    { (*this)[u].push_back(v); }

    void addUndirected(T u, T v)
    { addDirected(u, v); addDirected(v, u); }
};

class Range
{
    class RangeIterator
    {
    public:
        RangeIterator(int current = 0, int step = 1) : current(current), step(step) {}
        // works fine with c++11 range-based for loop ... only
        bool operator !=(const RangeIterator &iterator) const { return current < iterator.current; }
        int operator *() const { return current; }
        RangeIterator operator ++() { return {current += step, step}; }
    protected:
        int current, step;
    };
public:
    Range(int to, int from = 0, int step = 1) : from(from), to(to), step(step) {}
    RangeIterator begin() const { return {from, step}; }
    RangeIterator end() const { return {to, step}; }
protected:
    int from, to, step;
};

typedef Range range;
} // namespace Infinity::BattleLab

inline namespace IO
{
const char LF = '\n';
const char SP = ' ';

int dbl_prec = 10;
int ldbl_prec = 18;

inline void print(const int n)
{ printf("%d", n); }

inline void print(const unsigned n)
{ printf("%u", n); }

//#ifndef __i386__
inline void print(const long long n)
{ printf("%lld", n); }

inline void print(const unsigned long long n)
{ printf("%lld", n); }
//#endif

//inline void print(const int64_t n)
//{ printf("%" PRId64, n); }

//inline void print(const uint64_t n)
//{ printf("%" PRIu64, n); }

inline void print(const double a)
{ printf("%.*f", dbl_prec, a); }

inline void print(const long double a)
{ printf("%.*Lf", ldbl_prec, a); }

inline void print(const char c)
{ printf("%c", c); }

inline void print(const char s[])
{ printf("%s", s); }

inline void print(const string &s)
{ printf("%s", s.data()); }

template<typename T> inline void printSP(const T a)
{ print(a); print(SP); }

template<typename T, typename U> inline void print(const pair<T, U> &p)
{ printSP(p.first); print(p.second); }

template<class T> inline void print(const T a)
{ for (auto i = a.begin(); i != a.end(); i++) { if (i != a.begin()) print(SP); print(*i); } }

template<typename T> inline void print(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } }

template<typename T> inline void printr(T begin, T end)
{ for (print(*begin++); begin != end; ++begin) print(SP), print(*begin); }

inline void println()
{ print(LF); }

inline void printSP()
{ print(SP); }

template<typename T> inline void println(const T &a)
{ print(a); print(LF); }

template<typename T> inline void println(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } print(LF); }

template<class T> inline void printlns(const T &a)
{ for (auto n : a) println(n); }

template<typename T, typename... types> inline void print(const T &a, const types &...args)
{ print(a); print(args...); }

template<typename... types> inline void println(const types &...args)
{ print(args...); print(LF); }

template<typename... types> inline void printSP(const types &...args)
{ print(args...); print(SP); }

inline void printlnYN(bool b)
{ println(b ? "YES" : "NO"); }

inline void printlnyn(bool b)
{ println(b ? "Yes" : "No"); }

string caseN(int n, bool sharp = true, bool space = true)
{ return string("Case ") + (sharp ? "#" : "") + to_string(n) + ":" + (space ? " " : ""); }

inline int read(int &n)
{ return scanf("%d", &n); }

inline int read(int64_t &n)
{ return scanf("%" SCNd64, &n); }

template<typename T, typename ... types> inline int read(T &n, types &...args)
{ return read(n) == eof ? eof : read(args...) + 1; }

inline char getcc()
{ char c; do c = getchar(); while (isspace(c)); return c; }

inline int getint()
{ int n; read(n); return n; }

inline int64_t getll()
{ int64_t n; read(n); return n; }

inline double getdbl()
{ double n; scanf("%lf", &n); return n; }

inline pair<int, int> getpair()
{ pair<int, int> p; scanf("%d%d", &p.first, &p.second); return p; }

//inline vector<int> getints(int n)
//{ vector<int> v(n); for (int &i : v) i = getint(); return v; }

inline vector<pair<int, int> > getpairs(int n)
{ vector<pair<int, int> > v(n); for (pair<int, int> &p : v) { p = getpair(); } return v; }

inline void read(string &str, unsigned size)
{ char s[++size]; scanf("%s", s); str.assign(s); }

inline string getstr(unsigned size = 0x100000)
{ string s; read(s, size); return s; }

inline string getln(unsigned size = 0x100000)
{ char s[++size]; scanf("%[^\n]", s); getchar(); return s; }
} // namespace Infinity::IO
} // namespace Infinity

class BinaryIndexedTree
{
public:
    BinaryIndexedTree(int n) : a(n + 1) {}
    int sum(unsigned n) const { int s = 0; for (; n > 0; n -= -n & n) s += a[n]; return s; }
    int sum(unsigned l, unsigned r) const { return sum(r) - sum(l - 1); }
    void add(unsigned p, const int k) { if (p) for (; p < a.size(); p += -p & p) a[p] += k; }
protected:
    vector<int> a;
};

int main() /* Codeforces 830B */
{
    int n = getint();
    ints a = getints(n).argsort().add();
    a.segments(less<int>()).foreach([&](auto p){
        if (loop.first() == false) {
            rotate(&a[p.first], upper_bound(&a[p.first], &a[p.second], a[p.first - 1]), &a[p.second]);
        }
    });
    BinaryIndexedTree bit(n);
    for (int i = 1; i <= n; i++) {
        bit.add(i, 1);
    }
    int c = a[0], s = c;
    bit.add(a[0], -1);
    for (int i = 1; i < n; i++) {
        if (c < a[i]) {
            s += bit.sum(c, a[i]);
        } else {
            s += bit.sum(c, n) + bit.sum(a[i]);
        }
        c = a[i];
        bit.add(a[i], -1);
    }
    println(s);
    return 0;
}


// Infinity on Jul 15, 2017

#include <bits/stdc++.h>

using namespace std;

inline namespace Infinity
{
#define define_pair(classname, x, y) struct classname { int x; int y; \
    classname(int x = 0, int y = 0) : x(x), y(y) {} };
#define define_triple(classname, x, y, z) struct classname { int x; int y; int z; \
    classname(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} };
#define less_than(classname) bool operator <(ConstRef<classname> lhs, ConstRef<classname> rhs)
#define bool_lambda(type) [](const type &lhs, const type &rhs)

inline namespace Constant
{
constexpr const int eof         = EOF;
constexpr const int maxint      = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
constexpr const double eps      = DBL_EPSILON;
} // namespace Infinity::Constant

inline namespace TypeDefine
{
using uint  = unsigned int;
using ll    = long long int;
using ull   = unsigned long long int;
using ld    = long double;
template<typename T> using ConstRef = const T &;
template<typename T = int> using Pair = pair<T, T>;
template<typename T = int> using PairList = vector<Pair<T>>;
template<typename T = vector<int>> using Iter = typename T::iterator;
} // namespace Infinity::TypeDefine

inline namespace Miscelleneous
{
inline constexpr int ctoi(const char c)
{ return c - '0'; }

inline constexpr char itoc(const int n)
{ return n + '0'; }

inline int dtoi(const double d)
{ return round(d); }

template<typename T> inline constexpr bool in(T x, T l, T r)
{ return l <= x && x <= r; }

template<typename T> inline constexpr T tristate(int val, T nag, T zero, T pos)
{ return val > 0 ? pos : val < 0 ? nag : zero; }

template<class T> inline int size(const T &a)
{ return a.size(); }

template<class T> inline void sort(T &a)
{ sort(a.begin(), a.end()); }

template<class T1, class T2> inline void sort(T1 &a, T2 comp)
{ sort(a.begin(), a.end(), comp); }

template<class T1, typename T2> inline int lbound(const T1 &a, const T2 k)
{ return lower_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, typename T2> inline int ubound(const T1 &a, const T2 k)
{ return upper_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, class T2> inline int count(T1 &a, T2 k)
{ return ubound(a, k) - lbound(a, k); }

template<class T> inline void unique_erase(T &a)
{ a.erase(unique(a.begin(), a.end()), a.end()); }

template<typename T> inline void clear(T &a)
{ memset(a, 0, sizeof a); }

template<typename T> T gcd(T a, T b)
{ while (b) { T t = a % b; a = b; b = t; } return a; }

template<typename T> T lcm(T a, T b)
{ return a / gcd(a, b) * b; }

long long qpow(long long a, long long b, long long c)
{ return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; }

// find (x, y) s.t. a x + b y = gcd(a, b) = d
template<typename T> T exGcd(T a, T b, T &x, T &y)
{ T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; }

// returns number of nk's in [l, r]
template<typename T> inline constexpr T mps(T l, T r, T k)
{ return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; }

template<typename T> inline constexpr T sgn(T a)
{ return a ? a > 0 ? 1 : -1 : 0; }

template<typename T> inline constexpr T sq(T a)
{ return a * a; }

template<typename T> inline constexpr bool odd(T a)
{ return bool(a % 2); }

template<typename T> inline constexpr bool even(T a)
{ return !bool(a % 2); }

// simple mod
template<typename T1, typename T2> inline constexpr T1 smod(T1 x, T2 m)
{ return x > m ? x - m : x + m < m ? x + m : x; }

// lengthes of recurring period of replacement `a'
vector<int> repetend(vector<int> a)
{ vector<int> p(a.size(), -1);
  for (unsigned i = 0; i < a.size(); i++) if (p[i] == -1) {
    int c = 1; for (unsigned j = a[i]; j != i; j = a[j]) ++c;
    p[i] = c; for (unsigned j = a[i]; j != i; j = a[j]) p[j] = c;
  } return p; }

template<typename T> inline constexpr T lmiddle(T first, T last)
{ return first + (last - first) / 2; }

template<typename T> inline constexpr T rmiddle(T first, T last)
{ return last - (last - first) / 2; }

// returns the largest value in [l, r) s.t. check(x)
template<typename T, typename F> T dichotomy(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = lmiddle(l, r); (check(m) ? l : r) = m; } return l; }

// returns the smallest value in (l, r] s.t. check(x)
template<typename T, typename F> T dichotomy2(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = rmiddle(l, r); (check(m) ? r : l) = m; } return r; }
} // namespace Infinity::Miscelleneous

inline namespace BattleLab
{
template<typename T>
vector<int> argsort(const vector<T> &a)
{ vector<pair<T, int>> b; for (unsigned i = 0; i < a.size(); i++) { b.emplace_back(a[i], i); }
  stable_sort(b.begin(), b.end()); vector<int> c; for (pair<T, int> p : b) { c.push_back(p.second); }
  return c; }

template<typename T>
vector<T> rearrange(const vector<T> &a, const vector<int>& b)
{ vector<T> c; for (int i : b) { c.push_back(a[i]); } return c; }

// Array begins

struct
{
    int i, n;
    bool first() const { return i == 0; }
    bool last() const { return i == n - 1; }
} loop;

#if __cplusplus >= 201402L
#define STL_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return std::func(this->begin(), this->end(), args...); }
#define INFINITY_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return ::func(*this, args...); }
#define INFINITY_ALGO_OO_OVERRIDE(func, return_type) \
    template<typename... types> return_type func(types ...args) \
    { return ::func(*this, args...); }
#define PAST_PARTICIPLE(stem, pp) \
    template<typename... types> Array<T> &pp(types ...args) \
    { stem(args...); return *this; }
#endif

template<typename T>
class Array : public vector<T>
{
public:
    using vector<T>::vector;

    Array() = default;

    Array(const vector<T> &a) : vector<T>(a)
    {}

    int length() const
    { return this->size(); }

    template<typename... types> Array<T> &append(types ...args)
    { emplace_back(args...); return *this; }

    T sum() const
    { T s = T(); for (T val : *this) { s += val; } return s; }

    Array<T> &add(T increment = 1)
    { for (T &val : *this) { val += increment; } return *this; }

    Array<T> &unique()
    { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; }

    template<typename F> void foreach(F func)
    { loop.n = length(); for (loop.i = 0; loop.i < loop.n; loop.i++) { func(this->at(loop.i)); } }

    template<typename F> Array<pair<int, int>> segments(F func)
    { Array<pair<int, int>> a; int i = 0; while (i < length()) { int j = i + 1;
      while (j < length() && func(this->at(j - 1), this->at(j))) { j++; } a.emplace_back(i, j); i = j; } return a; }

#if defined(STL_ALGO_OO) && defined(PAST_PARTICIPLE)
    STL_ALGO_OO(sort)           PAST_PARTICIPLE(sort, sorted)
    STL_ALGO_OO(stable_sort)    PAST_PARTICIPLE(stable_sort, stable_sorted)
    STL_ALGO_OO(reverse)        PAST_PARTICIPLE(reverse, reversed)
    STL_ALGO_OO(lower_bound)    STL_ALGO_OO(next_permutation)
    STL_ALGO_OO(upper_bound)    STL_ALGO_OO(prev_permutation)
    STL_ALGO_OO(count)          STL_ALGO_OO(count_if)
    STL_ALGO_OO(find)           STL_ALGO_OO(find_if)
    STL_ALGO_OO(fill)           /*STL_ALGO_OO(for_each)*/
    STL_ALGO_OO(iota)           STL_ALGO_OO(partial_sum)
#endif
#if defined(INFINITY_ALGO_OO) && defined(INFINITY_ALGO_OO)
    INFINITY_ALGO_OO(lbound)    INFINITY_ALGO_OO(ubound)
    INFINITY_ALGO_OO_OVERRIDE(argsort, Array<int>)
#endif
};

Array<int> getints(unsigned size)
{ Array<int> a(size); for (int &x : a) { scanf("%d", &x); } return a; }

using ints = Array<int>;
using doubles = Array<double>;
using llongs = Array<long long>;

#undef STL_ALGO_OO
#undef INFINITY_ALGO_OO
#undef INFINITY_ALGO_OO_OVERRIDE
#undef PAST_PARTICIPLE

// Array ends

class Division
{
public:
    Division(int n, int k) : lb(n / k), ub(lb + 1), b(n % k)
    {}

    int operator[](int i)
    { return i < b ? ub : lb; }

protected:
    int lb, ub, b;
};

template<typename T>
class AdjacencyList : public vector<vector<T>>
{
public:
    AdjacencyList(unsigned size = 0) : vector<vector<T>>(size)
    {}

    void addDirected(T u, T v)
    { (*this)[u].push_back(v); }

    void addUndirected(T u, T v)
    { addDirected(u, v); addDirected(v, u); }
};

class Range
{
    class RangeIterator
    {
    public:
        RangeIterator(int current = 0, int step = 1) : current(current), step(step) {}
        // works fine with c++11 range-based for loop ... only
        bool operator !=(const RangeIterator &iterator) const { return current < iterator.current; }
        int operator *() const { return current; }
        RangeIterator operator ++() { return {current += step, step}; }
    protected:
        int current, step;
    };
public:
    Range(int to, int from = 0, int step = 1) : from(from), to(to), step(step) {}
    RangeIterator begin() const { return {from, step}; }
    RangeIterator end() const { return {to, step}; }
protected:
    int from, to, step;
};

typedef Range range;
} // namespace Infinity::BattleLab

inline namespace IO
{
const char LF = '\n';
const char SP = ' ';

int dbl_prec = 10;
int ldbl_prec = 18;

inline void print(const int n)
{ printf("%d", n); }

inline void print(const unsigned n)
{ printf("%u", n); }

//#ifndef __i386__
inline void print(const long long n)
{ printf("%lld", n); }

inline void print(const unsigned long long n)
{ printf("%lld", n); }
//#endif

//inline void print(const int64_t n)
//{ printf("%" PRId64, n); }

//inline void print(const uint64_t n)
//{ printf("%" PRIu64, n); }

inline void print(const double a)
{ printf("%.*f", dbl_prec, a); }

inline void print(const long double a)
{ printf("%.*Lf", ldbl_prec, a); }

inline void print(const char c)
{ printf("%c", c); }

inline void print(const char s[])
{ printf("%s", s); }

inline void print(const string &s)
{ printf("%s", s.data()); }

template<typename T> inline void printSP(const T a)
{ print(a); print(SP); }

template<typename T, typename U> inline void print(const pair<T, U> &p)
{ printSP(p.first); print(p.second); }

template<class T> inline void print(const T a)
{ for (auto i = a.begin(); i != a.end(); i++) { if (i != a.begin()) print(SP); print(*i); } }

template<typename T> inline void print(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } }

template<typename T> inline void printr(T begin, T end)
{ for (print(*begin++); begin != end; ++begin) print(SP), print(*begin); }

inline void println()
{ print(LF); }

inline void printSP()
{ print(SP); }

template<typename T> inline void println(const T &a)
{ print(a); print(LF); }

template<typename T> inline void println(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } print(LF); }

template<class T> inline void printlns(const T &a)
{ for (auto n : a) println(n); }

template<typename T, typename... types> inline void print(const T &a, const types &...args)
{ print(a); print(args...); }

template<typename... types> inline void println(const types &...args)
{ print(args...); print(LF); }

template<typename... types> inline void printSP(const types &...args)
{ print(args...); print(SP); }

inline void printlnYN(bool b)
{ println(b ? "YES" : "NO"); }

inline void printlnyn(bool b)
{ println(b ? "Yes" : "No"); }

string caseN(int n, bool sharp = true, bool space = true)
{ return string("Case ") + (sharp ? "#" : "") + to_string(n) + ":" + (space ? " " : ""); }

inline int read(int &n)
{ return scanf("%d", &n); }

inline int read(int64_t &n)
{ return scanf("%" SCNd64, &n); }

template<typename T, typename ... types> inline int read(T &n, types &...args)
{ return read(n) == eof ? eof : read(args...) + 1; }

inline char getcc()
{ char c; do c = getchar(); while (isspace(c)); return c; }

inline int getint()
{ int n; read(n); return n; }

inline int64_t getll()
{ int64_t n; read(n); return n; }

inline double getdbl()
{ double n; scanf("%lf", &n); return n; }

inline pair<int, int> getpair()
{ pair<int, int> p; scanf("%d%d", &p.first, &p.second); return p; }

//inline vector<int> getints(int n)
//{ vector<int> v(n); for (int &i : v) i = getint(); return v; }

inline vector<pair<int, int> > getpairs(int n)
{ vector<pair<int, int> > v(n); for (pair<int, int> &p : v) { p = getpair(); } return v; }

inline void read(string &str, unsigned size)
{ char s[++size]; scanf("%s", s); str.assign(s); }

inline string getstr(unsigned size = 0x100000)
{ string s; read(s, size); return s; }

inline string getln(unsigned size = 0x100000)
{ char s[++size]; scanf("%[^\n]", s); getchar(); return s; }
} // namespace Infinity::IO
} // namespace Infinity

class BinaryIndexedTree
{
public:
    BinaryIndexedTree(int n) : a(n + 1) {}
    int sum(unsigned n) const { int s = 0; for (; n > 0; n -= -n & n) s += a[n]; return s; }
    int sum(unsigned l, unsigned r) const { return sum(r) - sum(l - 1); }
    void add(unsigned p, const int k) { if (p) for (; p < a.size(); p += -p & p) a[p] += k; }
protected:
    vector<int> a;
};

int main() /* Codeforces 830B */
{
    int n = getint();
    ints a = getints(n).argsort().add();
    a.segments([&](int i, int j){
        return a[i] == a[j];
    }).foreach([&](auto p){
        if (loop.first() == false) {
            rotate(&a[p.first], upper_bound(&a[p.first], &a[p.second], a[p.first - 1]), &a[p.second]);
        }
    });
    BinaryIndexedTree bit(n);
    for (int i = 1; i <= n; i++) {
        bit.add(i, 1);
    }
    int c = a[0], s = c;
    bit.add(a[0], -1);
    for (int i = 1; i < n; i++) {
        if (c < a[i]) {
            s += bit.sum(c, a[i]);
        } else {
            s += bit.sum(c, n) + bit.sum(a[i]);
        }
        c = a[i];
        bit.add(a[i], -1);
    }
    println(s);
    return 0;
}


// Infinity on Jul 15, 2017

#include <bits/stdc++.h>

using namespace std;

inline namespace Infinity
{
#define define_pair(classname, x, y) struct classname { int x; int y; \
    classname(int x = 0, int y = 0) : x(x), y(y) {} };
#define define_triple(classname, x, y, z) struct classname { int x; int y; int z; \
    classname(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} };
#define less_than(classname) bool operator <(ConstRef<classname> lhs, ConstRef<classname> rhs)
#define bool_lambda(type) [](const type &lhs, const type &rhs)

inline namespace Constant
{
constexpr const int eof         = EOF;
constexpr const int maxint      = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
constexpr const double eps      = DBL_EPSILON;
} // namespace Infinity::Constant

inline namespace TypeDefine
{
using uint  = unsigned int;
using ll    = long long int;
using ull   = unsigned long long int;
using ld    = long double;
template<typename T> using ConstRef = const T &;
template<typename T = int> using Pair = pair<T, T>;
template<typename T = int> using PairList = vector<Pair<T>>;
template<typename T = vector<int>> using Iter = typename T::iterator;
} // namespace Infinity::TypeDefine

inline namespace Miscelleneous
{
inline constexpr int ctoi(const char c)
{ return c - '0'; }

inline constexpr char itoc(const int n)
{ return n + '0'; }

inline int dtoi(const double d)
{ return round(d); }

template<typename T> inline constexpr bool in(T x, T l, T r)
{ return l <= x && x <= r; }

template<typename T> inline constexpr T tristate(int val, T nag, T zero, T pos)
{ return val > 0 ? pos : val < 0 ? nag : zero; }

template<class T> inline int size(const T &a)
{ return a.size(); }

template<class T> inline void sort(T &a)
{ sort(a.begin(), a.end()); }

template<class T1, class T2> inline void sort(T1 &a, T2 comp)
{ sort(a.begin(), a.end(), comp); }

template<class T1, typename T2> inline int lbound(const T1 &a, const T2 k)
{ return lower_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, typename T2> inline int ubound(const T1 &a, const T2 k)
{ return upper_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, class T2> inline int count(T1 &a, T2 k)
{ return ubound(a, k) - lbound(a, k); }

template<class T> inline void unique_erase(T &a)
{ a.erase(unique(a.begin(), a.end()), a.end()); }

template<typename T> inline void clear(T &a)
{ memset(a, 0, sizeof a); }

template<typename T> T gcd(T a, T b)
{ while (b) { T t = a % b; a = b; b = t; } return a; }

template<typename T> T lcm(T a, T b)
{ return a / gcd(a, b) * b; }

long long qpow(long long a, long long b, long long c)
{ return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; }

// find (x, y) s.t. a x + b y = gcd(a, b) = d
template<typename T> T exGcd(T a, T b, T &x, T &y)
{ T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; }

// returns number of nk's in [l, r]
template<typename T> inline constexpr T mps(T l, T r, T k)
{ return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; }

template<typename T> inline constexpr T sgn(T a)
{ return a ? a > 0 ? 1 : -1 : 0; }

template<typename T> inline constexpr T sq(T a)
{ return a * a; }

template<typename T> inline constexpr bool odd(T a)
{ return bool(a % 2); }

template<typename T> inline constexpr bool even(T a)
{ return !bool(a % 2); }

// simple mod
template<typename T1, typename T2> inline constexpr T1 smod(T1 x, T2 m)
{ return x > m ? x - m : x + m < m ? x + m : x; }

// lengthes of recurring period of replacement `a'
vector<int> repetend(vector<int> a)
{ vector<int> p(a.size(), -1);
  for (unsigned i = 0; i < a.size(); i++) if (p[i] == -1) {
    int c = 1; for (unsigned j = a[i]; j != i; j = a[j]) ++c;
    p[i] = c; for (unsigned j = a[i]; j != i; j = a[j]) p[j] = c;
  } return p; }

template<typename T> inline constexpr T lmiddle(T first, T last)
{ return first + (last - first) / 2; }

template<typename T> inline constexpr T rmiddle(T first, T last)
{ return last - (last - first) / 2; }

// returns the largest value in [l, r) s.t. check(x)
template<typename T, typename F> T dichotomy(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = lmiddle(l, r); (check(m) ? l : r) = m; } return l; }

// returns the smallest value in (l, r] s.t. check(x)
template<typename T, typename F> T dichotomy2(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = rmiddle(l, r); (check(m) ? r : l) = m; } return r; }
} // namespace Infinity::Miscelleneous

inline namespace BattleLab
{
template<typename T>
vector<int> argsort(const vector<T> &a)
{ vector<pair<T, int>> b; for (unsigned i = 0; i < a.size(); i++) { b.emplace_back(a[i], i); }
  stable_sort(b.begin(), b.end()); vector<int> c; for (pair<T, int> p : b) { c.push_back(p.second); }
  return c; }

template<typename T>
vector<T> rearrange(const vector<T> &a, const vector<int>& b)
{ vector<T> c; for (int i : b) { c.push_back(a[i]); } return c; }

// Array begins

struct
{
    int i, n;
    bool first() const { return i == 0; }
    bool last() const { return i == n - 1; }
} loop;

#if __cplusplus >= 201402L
#define STL_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return std::func(this->begin(), this->end(), args...); }
#define INFINITY_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return ::func(*this, args...); }
#define INFINITY_ALGO_OO_OVERRIDE(func, return_type) \
    template<typename... types> return_type func(types ...args) \
    { return ::func(*this, args...); }
#define PAST_PARTICIPLE(stem, pp) \
    template<typename... types> Array<T> &pp(types ...args) \
    { stem(args...); return *this; }
#endif

template<typename T>
class Array : public vector<T>
{
public:
    using vector<T>::vector;

    Array() = default;

    Array(const vector<T> &a) : vector<T>(a)
    {}

    int length() const
    { return this->size(); }

    template<typename... types> Array<T> &append(types ...args)
    { emplace_back(args...); return *this; }

    T sum() const
    { T s = T(); for (T val : *this) { s += val; } return s; }

    Array<T> &add(T increment = 1)
    { for (T &val : *this) { val += increment; } return *this; }

    Array<T> &unique()
    { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; }

    template<typename F> void foreach(F func)
    { loop.n = length(); for (loop.i = 0; loop.i < loop.n; loop.i++) { func(this->at(loop.i)); } }

    template<typename F> Array<pair<int, int>> segments(F func)
    { Array<pair<int, int>> a; int i = 0; while (i < length()) { int j = i + 1;
      while (j < length() && func(this->at(j - 1), this->at(j))) { j++; } a.emplace_back(i, j); i = j; } return a; }

#if defined(STL_ALGO_OO) && defined(PAST_PARTICIPLE)
    STL_ALGO_OO(sort)           PAST_PARTICIPLE(sort, sorted)
    STL_ALGO_OO(stable_sort)    PAST_PARTICIPLE(stable_sort, stable_sorted)
    STL_ALGO_OO(reverse)        PAST_PARTICIPLE(reverse, reversed)
    STL_ALGO_OO(lower_bound)    STL_ALGO_OO(next_permutation)
    STL_ALGO_OO(upper_bound)    STL_ALGO_OO(prev_permutation)
    STL_ALGO_OO(count)          STL_ALGO_OO(count_if)
    STL_ALGO_OO(find)           STL_ALGO_OO(find_if)
    STL_ALGO_OO(fill)           /*STL_ALGO_OO(for_each)*/
    STL_ALGO_OO(iota)           STL_ALGO_OO(partial_sum)
#endif
#if defined(INFINITY_ALGO_OO) && defined(INFINITY_ALGO_OO)
    INFINITY_ALGO_OO(lbound)    INFINITY_ALGO_OO(ubound)
    INFINITY_ALGO_OO_OVERRIDE(argsort, Array<int>)
#endif
};

Array<int> getints(unsigned size)
{ Array<int> a(size); for (int &x : a) { scanf("%d", &x); } return a; }

using ints = Array<int>;
using doubles = Array<double>;
using llongs = Array<long long>;

#undef STL_ALGO_OO
#undef INFINITY_ALGO_OO
#undef INFINITY_ALGO_OO_OVERRIDE
#undef PAST_PARTICIPLE

// Array ends

class Division
{
public:
    Division(int n, int k) : lb(n / k), ub(lb + 1), b(n % k)
    {}

    int operator[](int i)
    { return i < b ? ub : lb; }

protected:
    int lb, ub, b;
};

template<typename T>
class AdjacencyList : public vector<vector<T>>
{
public:
    AdjacencyList(unsigned size = 0) : vector<vector<T>>(size)
    {}

    void addDirected(T u, T v)
    { (*this)[u].push_back(v); }

    void addUndirected(T u, T v)
    { addDirected(u, v); addDirected(v, u); }
};

class Range
{
    class RangeIterator
    {
    public:
        RangeIterator(int current = 0, int step = 1) : current(current), step(step) {}
        // works fine with c++11 range-based for loop ... only
        bool operator !=(const RangeIterator &iterator) const { return current < iterator.current; }
        int operator *() const { return current; }
        RangeIterator operator ++() { return {current += step, step}; }
    protected:
        int current, step;
    };
public:
    Range(int to, int from = 0, int step = 1) : from(from), to(to), step(step) {}
    RangeIterator begin() const { return {from, step}; }
    RangeIterator end() const { return {to, step}; }
protected:
    int from, to, step;
};

typedef Range range;
} // namespace Infinity::BattleLab

inline namespace IO
{
const char LF = '\n';
const char SP = ' ';

int dbl_prec = 10;
int ldbl_prec = 18;

inline void print(const int n)
{ printf("%d", n); }

inline void print(const unsigned n)
{ printf("%u", n); }

//#ifndef __i386__
inline void print(const long long n)
{ printf("%lld", n); }

inline void print(const unsigned long long n)
{ printf("%lld", n); }
//#endif

//inline void print(const int64_t n)
//{ printf("%" PRId64, n); }

//inline void print(const uint64_t n)
//{ printf("%" PRIu64, n); }

inline void print(const double a)
{ printf("%.*f", dbl_prec, a); }

inline void print(const long double a)
{ printf("%.*Lf", ldbl_prec, a); }

inline void print(const char c)
{ printf("%c", c); }

inline void print(const char s[])
{ printf("%s", s); }

inline void print(const string &s)
{ printf("%s", s.data()); }

template<typename T> inline void printSP(const T a)
{ print(a); print(SP); }

template<typename T, typename U> inline void print(const pair<T, U> &p)
{ printSP(p.first); print(p.second); }

template<class T> inline void print(const T a)
{ for (auto i = a.begin(); i != a.end(); i++) { if (i != a.begin()) print(SP); print(*i); } }

template<typename T> inline void print(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } }

template<typename T> inline void printr(T begin, T end)
{ for (print(*begin++); begin != end; ++begin) print(SP), print(*begin); }

inline void println()
{ print(LF); }

inline void printSP()
{ print(SP); }

template<typename T> inline void println(const T &a)
{ print(a); print(LF); }

template<typename T> inline void println(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } print(LF); }

template<class T> inline void printlns(const T &a)
{ for (auto n : a) println(n); }

template<typename T, typename... types> inline void print(const T &a, const types &...args)
{ print(a); print(args...); }

template<typename... types> inline void println(const types &...args)
{ print(args...); print(LF); }

template<typename... types> inline void printSP(const types &...args)
{ print(args...); print(SP); }

inline void printlnYN(bool b)
{ println(b ? "YES" : "NO"); }

inline void printlnyn(bool b)
{ println(b ? "Yes" : "No"); }

string caseN(int n, bool sharp = true, bool space = true)
{ return string("Case ") + (sharp ? "#" : "") + to_string(n) + ":" + (space ? " " : ""); }

inline int read(int &n)
{ return scanf("%d", &n); }

inline int read(int64_t &n)
{ return scanf("%" SCNd64, &n); }

template<typename T, typename ... types> inline int read(T &n, types &...args)
{ return read(n) == eof ? eof : read(args...) + 1; }

inline char getcc()
{ char c; do c = getchar(); while (isspace(c)); return c; }

inline int getint()
{ int n; read(n); return n; }

inline int64_t getll()
{ int64_t n; read(n); return n; }

inline double getdbl()
{ double n; scanf("%lf", &n); return n; }

inline pair<int, int> getpair()
{ pair<int, int> p; scanf("%d%d", &p.first, &p.second); return p; }

//inline vector<int> getints(int n)
//{ vector<int> v(n); for (int &i : v) i = getint(); return v; }

inline vector<pair<int, int> > getpairs(int n)
{ vector<pair<int, int> > v(n); for (pair<int, int> &p : v) { p = getpair(); } return v; }

inline void read(string &str, unsigned size)
{ char s[++size]; scanf("%s", s); str.assign(s); }

inline string getstr(unsigned size = 0x100000)
{ string s; read(s, size); return s; }

inline string getln(unsigned size = 0x100000)
{ char s[++size]; scanf("%[^\n]", s); getchar(); return s; }
} // namespace Infinity::IO
} // namespace Infinity

class BinaryIndexedTree
{
public:
    BinaryIndexedTree(int n) : a(n + 1) {}
    int sum(unsigned n) const { int s = 0; for (; n > 0; n -= -n & n) s += a[n]; return s; }
    int sum(unsigned l, unsigned r) const { return sum(r) - sum(l - 1); }
    void add(unsigned p, const int k) { if (p) for (; p < a.size(); p += -p & p) a[p] += k; }
protected:
    vector<int> a;
};

int main() /* Codeforces 830B */
{
    int n = getint();
    ints a = getints(n);
    ints b = a.argsort().add();
    b.segments([&](int i, int j){
        return a[i - 1] == a[j - 1];
    }).foreach([&](auto p){
        if (loop.first() == false) {
            rotate(&b[p.first], upper_bound(&b[p.first], &b[p.second], b[p.first - 1]), &b[p.second]);
        }
    });
    BinaryIndexedTree bit(n);
    for (int i = 1; i <= n; i++) {
        bit.add(i, 1);
    }
    int c = b[0], s = c;
    bit.add(b[0], -1);
    for (int i = 1; i < n; i++) {
        if (c < b[i]) {
            s += bit.sum(c, b[i]);
        } else {
            s += bit.sum(c, n) + bit.sum(b[i]);
        }
        c = b[i];
        bit.add(b[i], -1);
    }
    println(s);
    return 0;
}


#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
//#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005],ll=0;
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}

#define N 100005
int nt[2*N];//,ar[N];
void build(int n)
{	f(i,1,n+1)
		nt[i]=(i&(-i));
}

void upd(int n,int i)
{	nt[i]--;i+=(i&(-i));
	while(i<=n)
	{	nt[i]--;
		i+=(i&(-i));
	}
}
inline int query(int l,int r)
{	int on=0,tw=0;l--;
	while(l)
	{	on+=nt[l];
		l-=(l&(-l));
	}
	while(r)
	{	tw+=nt[r];
		r-=(r&(-r));
	}
	return tw-on;
}
map<int,int> mm;
vi vec[100005];
int main()
{	int n;
	cin>>n;
//	f(i,1,n+1)vec[i].clear();
	f(i,1,n+1)
	{	sc(ar[i]);
		if(mm.find(ar[i])==mm.end())
			mm[ar[i]]=ll++;
		vec[mm[ar[i]]].pb(i);
	}
	int pr=1,l,tmp;
	LL an=0;
	build(n);
	trv(mm,it)
	{	tmp=it.S;
		if(vec[tmp].empty())
			continue;
		if(pr>vec[tmp].back())
		{	an+=query(pr+1,n);
			l=0;pr=1;
		}
		else
			l=lb(all(vec[tmp]),pr)-vec[tmp].begin();
		trace(pr,vec[tmp].back());
		an+=query(pr,vec[tmp].back());
		pr=vec[tmp].back();
		if(l!=0)
		{	trace(pr,l,vec[tmp][l-1]);
			an+=query(pr+1,n) + query(1,vec[tmp][l-1]);
			pr=vec[tmp][l-1];
		}
		f(i,0,vec[tmp].size())
		{
//		if(vec[tmp][i]>=pr)
//			trace(query(pr,vec[tmp][i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,vec[tmp].S);
		upd(n,vec[tmp][i]);
//		trace(vec[tmp][i],(*(mm.begin())).F);
		}
	}
	cout<<an<<endl;
}


#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
//#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005];
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}
map<int,vi> mm;
int main()
{	int n;
	cin>>n;
	f(i,0,n)
	{	sc(ar[i]);
		mm[ar[i]].pb(i);
	}
	trv(mm,it)
		sort(all(it.S));
	auto tmp=mm[ar[0]];int pr=0,l;
	LL an=0;
	build(0,n-1,1);
	while(!mm.empty())
	{	tmp=(*(mm.begin())).S;
		l=lb(all(tmp),pr)-tmp.begin();
		if(l==tmp.size())
		{	an+=query(0,n-1,1,pr,n-1);
			l=0;pr=0;
		}
		f(i,l,tmp.size()){
//		if(tmp[i]>=pr)
			an+=query(0,n-1,1,pr,tmp[i]);
			trace(query(0,n-1,1,pr,tmp[i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,tmp.S);
		pr=tmp[i];
		update(0,n-1,1,tmp[i]);
		trace(tmp[i],(*(mm.begin())).F);
		}
		if(l==0)mm.erase(mm.begin());
	}
	cout<<an<<endl;
}