//Love and Freedom.
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 20021225
#define N 200010
using namespace std;
int read()
{
	int s=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return f*s;
}
ll pre[N]; int a[N],b[N],v[N],n;
int main()
{
	n=read();
	for(int i=1;i<=n;i++)	v[i]=a[i]=read(),v[n+i]=b[i]=read();
	sort(v+1,v+2*n+1);
	for(int i=1;i<=n;i++)	pre[i]=pre[i-1]+v[i];
	ll ans=(ll)1e18,ta=0,tb=0;
	for(int i=1;i<=n;i++)
	{
		ll tmp; ta+=a[i],tb+=b[i];
		if(max(a[i],b[i])<v[n])	tmp=pre[n];
		else if(min(a[i],b[i])<v[n])	tmp=pre[n-1]+max(a[i],b[i]);
		else	tmp=pre[n-2]+a[i]+b[i];
		ans=min(ans,tmp); 
	}
	ans=min(min(ta,tb),ans);
	printf("%lld\n",ans);
	return 0;
}

### Prompt

Your challenge is to write 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
//Love and Freedom.
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 20021225
#define N 200010
using namespace std;
int read()
{
	int s=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return f*s;
}
ll pre[N]; int a[N],b[N],v[N],n;
int main()
{
	n=read();
	for(int i=1;i<=n;i++)	v[i]=a[i]=read(),v[n+i]=b[i]=read();
	sort(v+1,v+2*n+1);
	for(int i=1;i<=n;i++)	pre[i]=pre[i-1]+v[i];
	ll ans=(ll)1e18,ta=0,tb=0;
	for(int i=1;i<=n;i++)
	{
		ll tmp; ta+=a[i],tb+=b[i];
		if(max(a[i],b[i])<v[n])	tmp=pre[n];
		else if(min(a[i],b[i])<v[n])	tmp=pre[n-1]+max(a[i],b[i]);
		else	tmp=pre[n-2]+a[i]+b[i];
		ans=min(ans,tmp); 
	}
	ans=min(min(ta,tb),ans);
	printf("%lld\n",ans);
	return 0;
}

```