#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N],b[N],n,ans,s1,s2,s;
pair<ll,int> x1,x2,i1,i2;
vector<pair<ll,int> > h;
int cnt[N];
int main(){
	cin>>n;
	i1=i2=make_pair(2e9,0);
	for (int i=1;i<=n;i++){
		scanf("%lld%lld",&a[i],&b[i]);
		s1+=a[i],s2+=b[i];
		s+=min(a[i],b[i]);
		pair<ll,int> z=make_pair(max(a[i],b[i]),i);
		if (z<i1)swap(z,i1);
		if (z<i2)i2=z;
		z=make_pair(min(a[i],b[i]),i);
		if (z>x1)swap(z,x1);
		if (z>x2)x2=z;
		h.push_back(make_pair(a[i],i));
		h.push_back(make_pair(b[i],i));
	}
	sort(h.begin(),h.end());
	int can=0;
	ll sum=0;
	for (int i=0;i<n;i++){
		cnt[h[i].second]++;
		if (cnt[h[i].second]==2)can=1;
		sum+=h[i].first;
	}
	if (can){
		cout<<sum<<endl;
		return 0;
	}
	ans=i1.second==x1.second?(min(i2.first-x1.first,i1.first-x2.first)):i1.first-x1.first;
	ans=min(min(s1,s2),ans+s);
	cout<<ans<<endl;
}
### Prompt

In CPP, your task is to solve 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>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N],b[N],n,ans,s1,s2,s;
pair<ll,int> x1,x2,i1,i2;
vector<pair<ll,int> > h;
int cnt[N];
int main(){
	cin>>n;
	i1=i2=make_pair(2e9,0);
	for (int i=1;i<=n;i++){
		scanf("%lld%lld",&a[i],&b[i]);
		s1+=a[i],s2+=b[i];
		s+=min(a[i],b[i]);
		pair<ll,int> z=make_pair(max(a[i],b[i]),i);
		if (z<i1)swap(z,i1);
		if (z<i2)i2=z;
		z=make_pair(min(a[i],b[i]),i);
		if (z>x1)swap(z,x1);
		if (z>x2)x2=z;
		h.push_back(make_pair(a[i],i));
		h.push_back(make_pair(b[i],i));
	}
	sort(h.begin(),h.end());
	int can=0;
	ll sum=0;
	for (int i=0;i<n;i++){
		cnt[h[i].second]++;
		if (cnt[h[i].second]==2)can=1;
		sum+=h[i].first;
	}
	if (can){
		cout<<sum<<endl;
		return 0;
	}
	ans=i1.second==x1.second?(min(i2.first-x1.first,i1.first-x2.first)):i1.first-x1.first;
	ans=min(min(s1,s2),ans+s);
	cout<<ans<<endl;
}
```