#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
typedef pair<int,int> pir;
const int N=100010;
bool w[N];
int a[N],b[N],p[N],q[N];
inline int gi() {
    int x=0,o=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=='-'?o=-1:0,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return o*x;
}
int main() {
    int n;
    long long s1=0,s2=0,ans=1LL<<60,sum=0;
    cin>>n;
    for(int i=1;i<=n;i++) {
	a[i]=gi(),b[i]=gi(),s1+=a[i],s2+=b[i];
	if(a[i]>b[i]) swap(a[i],b[i]);
	sum+=a[i],p[i]=q[i]=i;
    }
    sort(p+1,p+1+n,[](const int &x,const int &y) {return b[x]<b[y];});
    sort(q+1,q+1+n,[](const int &x,const int &y) {return a[x]>a[y];});
    for(int i=1,j=1;;i++,j++) {
	while(w[p[i]]) ++i;
	while(w[q[j]]) ++j;
	if(p[i]==q[j]) {
	    if(i==n||(j<n&&b[p[i+1]]-b[p[i]]>a[q[j]]-a[q[j+1]])) ++j;
	    else ++i;
	}
	if(i>n||j>n) break;
	sum+=b[p[i]]-a[q[j]];
	ans=min(ans,sum),w[p[i]]=w[q[j]]=1;
    }
    cout<<min(ans,min(s1,s2));
    return 0;
}
### 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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
typedef pair<int,int> pir;
const int N=100010;
bool w[N];
int a[N],b[N],p[N],q[N];
inline int gi() {
    int x=0,o=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=='-'?o=-1:0,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return o*x;
}
int main() {
    int n;
    long long s1=0,s2=0,ans=1LL<<60,sum=0;
    cin>>n;
    for(int i=1;i<=n;i++) {
	a[i]=gi(),b[i]=gi(),s1+=a[i],s2+=b[i];
	if(a[i]>b[i]) swap(a[i],b[i]);
	sum+=a[i],p[i]=q[i]=i;
    }
    sort(p+1,p+1+n,[](const int &x,const int &y) {return b[x]<b[y];});
    sort(q+1,q+1+n,[](const int &x,const int &y) {return a[x]>a[y];});
    for(int i=1,j=1;;i++,j++) {
	while(w[p[i]]) ++i;
	while(w[q[j]]) ++j;
	if(p[i]==q[j]) {
	    if(i==n||(j<n&&b[p[i+1]]-b[p[i]]>a[q[j]]-a[q[j+1]])) ++j;
	    else ++i;
	}
	if(i>n||j>n) break;
	sum+=b[p[i]]-a[q[j]];
	ans=min(ans,sum),w[p[i]]=w[q[j]]=1;
    }
    cout<<min(ans,min(s1,s2));
    return 0;
}
```