#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
typedef pair<int,int> pa;
typedef vector<int> vec;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
bool ok,ap[500010];
LL A,B,t,ans;
pa tmp[500010];
int ts,n,a[500010],b[500010];
int main(){
	cin>>n;
	FOR(i,1,n){
		getint(a[i]),getint(b[i]);
		tmp[++ts]=mp(a[i],i);
		tmp[++ts]=mp(b[i],i);
		A+=a[i];
		B+=b[i];
	}
	ans=min(A,B);
	sort(tmp+1,tmp+ts+1);
	FOR(i,1,n){
		if (ap[tmp[i].se]) ok=1;
		ap[tmp[i].se]=1;
	}
	t=0;
	FOR(i,1,n) t+=tmp[i].fi;
	if (ok) return cout<<t<<endl,0;
	FOR(i,1,n){
		LL tt=t-tmp[i].fi;
		FOR(j,n+1,n+n)
			if (tmp[j].se!=tmp[i].se){
				ans=min(ans,tt+tmp[j].fi);
				break;
			}
	}
	cout<<ans<<endl;
	return 0;
}
### Prompt

Your task is to create a cpp solution to the following problem:

We have a directed weighted graph with N vertices. Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i.

In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N, and its weight is {\rm min}(A_x,B_y).

We will consider a directed cycle in this graph that visits every vertex exactly once. Find the minimum total weight of the edges in such a cycle.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
* 1 \leq B_i \leq 10^9
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 B_1
A_2 B_2
:
A_N B_N


Output

Print the minimum total weight of the edges in such a cycle.

Examples

Input

3
1 5
4 2
6 3


Output

7


Input

4
1 5
2 6
3 7
4 8


Output

10


Input

6
19 92
64 64
78 48
57 33
73 6
95 73


Output

227

### Response

```cpp
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
typedef pair<int,int> pa;
typedef vector<int> vec;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
bool ok,ap[500010];
LL A,B,t,ans;
pa tmp[500010];
int ts,n,a[500010],b[500010];
int main(){
	cin>>n;
	FOR(i,1,n){
		getint(a[i]),getint(b[i]);
		tmp[++ts]=mp(a[i],i);
		tmp[++ts]=mp(b[i],i);
		A+=a[i];
		B+=b[i];
	}
	ans=min(A,B);
	sort(tmp+1,tmp+ts+1);
	FOR(i,1,n){
		if (ap[tmp[i].se]) ok=1;
		ap[tmp[i].se]=1;
	}
	t=0;
	FOR(i,1,n) t+=tmp[i].fi;
	if (ok) return cout<<t<<endl,0;
	FOR(i,1,n){
		LL tt=t-tmp[i].fi;
		FOR(j,n+1,n+n)
			if (tmp[j].se!=tmp[i].se){
				ans=min(ans,tt+tmp[j].fi);
				break;
			}
	}
	cout<<ans<<endl;
	return 0;
}
```