#include <bits/stdc++.h>
#define M 1000005
#define FI first
#define SE second
#define MP std::make_pair
#define PB push_back
typedef long long ll;
int n;
std::pair <int,int> S[M];
bool vis[M];
int read(){
	char c=getchar();int ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
void Write(ll x){
	if (x<10) putchar(x^48);
	else Write(x/10),putchar((x%10)^48);
	return;
}
ll min(ll x,ll y){return x<y?x:y;}
bool check(){
	int flag1=0,flag2=0;
	for (int i=1;i<=n;i++)
		if (vis[i]&&vis[i+n]) return 1;
	for (int i=1;i<=n;i++)
		flag1|=vis[i],flag2|=vis[i+n];
	return flag1^flag2;
}
int main(){
	n=read();
	for (int i=1;i<=n;i++) S[i*2-1].FI=read(),S[i*2-1].SE=i,S[i*2].FI=read(),S[i*2].SE=i+n;
	std::sort(S+1,S+1+n*2);ll sum=0;
	for (int i=1;i<=n;i++) vis[S[i].SE]=1,sum+=S[i].FI;
	if (check()) return Write(sum),0;
	vis[S[n].SE]=0,vis[S[n+1].SE]=1;
	if (check()) return Write(sum-S[n].FI+S[n+1].FI),0;
	Write(min(sum-S[n-1].FI+S[n+1].FI,sum-S[n].FI+S[n+2].FI));
	return 0;
}

### Prompt

Generate 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
#include <bits/stdc++.h>
#define M 1000005
#define FI first
#define SE second
#define MP std::make_pair
#define PB push_back
typedef long long ll;
int n;
std::pair <int,int> S[M];
bool vis[M];
int read(){
	char c=getchar();int ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
void Write(ll x){
	if (x<10) putchar(x^48);
	else Write(x/10),putchar((x%10)^48);
	return;
}
ll min(ll x,ll y){return x<y?x:y;}
bool check(){
	int flag1=0,flag2=0;
	for (int i=1;i<=n;i++)
		if (vis[i]&&vis[i+n]) return 1;
	for (int i=1;i<=n;i++)
		flag1|=vis[i],flag2|=vis[i+n];
	return flag1^flag2;
}
int main(){
	n=read();
	for (int i=1;i<=n;i++) S[i*2-1].FI=read(),S[i*2-1].SE=i,S[i*2].FI=read(),S[i*2].SE=i+n;
	std::sort(S+1,S+1+n*2);ll sum=0;
	for (int i=1;i<=n;i++) vis[S[i].SE]=1,sum+=S[i].FI;
	if (check()) return Write(sum),0;
	vis[S[n].SE]=0,vis[S[n+1].SE]=1;
	if (check()) return Write(sum-S[n].FI+S[n+1].FI),0;
	Write(min(sum-S[n-1].FI+S[n+1].FI,sum-S[n].FI+S[n+2].FI));
	return 0;
}

```