#include<bits/stdc++.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int t,i,j,n,x,a[maxn],b[maxn],p1[maxn],p2[maxn];ll ans,sum,sa,sb,s;
int mi,cmi,z1,z2;
bool bz[maxn],B;
bool cmp1(int x,int y){return a[x]>a[y];}
bool cmp2(int x,int y){return b[x]>b[y];}
int main(){
	scanf("%d",&n);
	fo(i,1,n) scanf("%d%d",&a[i],&b[i]),p1[i]=p2[i]=i,sa+=a[i],sb+=b[i];
	sum=sa+sb;
	sort(p1+1,p1+1+n,cmp1);
	sort(p2+1,p2+1+n,cmp2);
	i=j=1;
	fo(t,1,n){
		if (i<=n&&a[p1[i]]>=b[p2[j]]) ans+=a[x=p1[i++]];else ans+=b[x=p2[j++]];
		if (t==n) mi=x;
		if (t==n-1) cmi=x;
		B|=bz[x],bz[x]=1;
	}
	z1=max(a[mi],b[mi]),z2=max(a[cmi],b[cmi]);
	if (!B){
		s=-2e9;
		fo(i,1,n){
			if (a[i]>=b[i]) s=max(s,b[i]-(i!=mi?z1:z2));
			else s=max(s,a[i]-(i!=mi?z1:z2));
		}
		ans+=s;
		ans=max(ans,sa);
		ans=max(ans,sb);
	}
	printf("%lld\n",sum-ans);
}
### Prompt

Construct a CPP code solution to the problem outlined:

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 fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int t,i,j,n,x,a[maxn],b[maxn],p1[maxn],p2[maxn];ll ans,sum,sa,sb,s;
int mi,cmi,z1,z2;
bool bz[maxn],B;
bool cmp1(int x,int y){return a[x]>a[y];}
bool cmp2(int x,int y){return b[x]>b[y];}
int main(){
	scanf("%d",&n);
	fo(i,1,n) scanf("%d%d",&a[i],&b[i]),p1[i]=p2[i]=i,sa+=a[i],sb+=b[i];
	sum=sa+sb;
	sort(p1+1,p1+1+n,cmp1);
	sort(p2+1,p2+1+n,cmp2);
	i=j=1;
	fo(t,1,n){
		if (i<=n&&a[p1[i]]>=b[p2[j]]) ans+=a[x=p1[i++]];else ans+=b[x=p2[j++]];
		if (t==n) mi=x;
		if (t==n-1) cmi=x;
		B|=bz[x],bz[x]=1;
	}
	z1=max(a[mi],b[mi]),z2=max(a[cmi],b[cmi]);
	if (!B){
		s=-2e9;
		fo(i,1,n){
			if (a[i]>=b[i]) s=max(s,b[i]-(i!=mi?z1:z2));
			else s=max(s,a[i]-(i!=mi?z1:z2));
		}
		ans+=s;
		ans=max(ans,sa);
		ans=max(ans,sb);
	}
	printf("%lld\n",sum-ans);
}
```