#include <bits/stdc++.h>
int n,f[2][500005],ff[500005],f1,f2;
long long sum,sum1,sum2;
struct note{
	int x,id;
}a[1000005];
bool cmp(note x,note y){
	return x.x<y.x;
} 
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i+n].x);
		f[0][i]=a[i].x,f[1][i]=a[i+n].x;
		sum1+=a[i].x,sum2+=a[i+n].x; 
		a[i].id=i,a[i+n].id=i+n; 
	}
	std::sort(a+1,a+2*n+1,cmp);
	for (int i=1;i<=n;i++) sum=sum+a[i].x;
	for (int j=1;j<=n;j++) {
		int i=a[j].id;
		if (i>n){
			if (ff[i-n]){
				printf("%lld\n",sum);
				return 0;
			}
			ff[i-n]=1,f2=1;
		}
		else {
			if (ff[i]){
				printf("%lld\n",sum);
				return 0; 
			}
			ff[i]=1,f1=1;
		}
	}
	if(f1==0 || f2==0) {
		printf("%lld\n",sum);
		return 0;
	}
	int m=1000000000;
	for (int i=n+1;i<=2*n;i++){
		int j=a[i].id-1;
		if (j%n!=(a[n].id-1)%n) m=std::min(m,f[j/n][j%n+1]-a[n].x);
		else m=std::min(m,f[j/n][j%n+1]-a[n-1].x)	;
	}
	printf("%lld",std::min(sum+m,std::min(sum1,sum2))); 
} 
### Prompt

Develop a solution in CPP to the problem described below:

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>
int n,f[2][500005],ff[500005],f1,f2;
long long sum,sum1,sum2;
struct note{
	int x,id;
}a[1000005];
bool cmp(note x,note y){
	return x.x<y.x;
} 
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i+n].x);
		f[0][i]=a[i].x,f[1][i]=a[i+n].x;
		sum1+=a[i].x,sum2+=a[i+n].x; 
		a[i].id=i,a[i+n].id=i+n; 
	}
	std::sort(a+1,a+2*n+1,cmp);
	for (int i=1;i<=n;i++) sum=sum+a[i].x;
	for (int j=1;j<=n;j++) {
		int i=a[j].id;
		if (i>n){
			if (ff[i-n]){
				printf("%lld\n",sum);
				return 0;
			}
			ff[i-n]=1,f2=1;
		}
		else {
			if (ff[i]){
				printf("%lld\n",sum);
				return 0; 
			}
			ff[i]=1,f1=1;
		}
	}
	if(f1==0 || f2==0) {
		printf("%lld\n",sum);
		return 0;
	}
	int m=1000000000;
	for (int i=n+1;i<=2*n;i++){
		int j=a[i].id-1;
		if (j%n!=(a[n].id-1)%n) m=std::min(m,f[j/n][j%n+1]-a[n].x);
		else m=std::min(m,f[j/n][j%n+1]-a[n-1].x)	;
	}
	printf("%lld",std::min(sum+m,std::min(sum1,sum2))); 
} 
```