#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
static const int MAX_N = 100000;

int N;
P p[2 * MAX_N];

int main(){
	scanf("%d", &N);
	ll res1 = 0, res2 = 0, res3 = 0;
	for(int i = 0; i < N; i++){
		scanf("%lld %lld", &p[2 * i].first, &p[2 * i + 1].first);
		res1 += p[2 * i].first, res2 += p[2 * i + 1].first;
		p[2 * i].second = i; p[2 * i + 1].second = i;
	}
	sort(p, p + 2 * N);
	int num[MAX_N];
	fill(num, num + N, 0);
	bool flag = true;
	int index = -1;
	for(int i = 0; i < N; i++){
		res3 += p[i].first;
		num[p[i].second]++;
		if(num[p[i].second] >= 2) flag = false;
	}
	if(flag){
		if(p[N - 1].second != p[N].second) res3 = res3 - p[N - 1].first + p[N].first;
		else res3 = res3 - max(p[N - 1].first - p[N + 1].first, p[N - 2].first - p[N].first);
	}
	printf("%lld\n", min(min(res1, res2), res3));
	return 0;
}

### Prompt

Please provide a Cpp coded solution 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<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
static const int MAX_N = 100000;

int N;
P p[2 * MAX_N];

int main(){
	scanf("%d", &N);
	ll res1 = 0, res2 = 0, res3 = 0;
	for(int i = 0; i < N; i++){
		scanf("%lld %lld", &p[2 * i].first, &p[2 * i + 1].first);
		res1 += p[2 * i].first, res2 += p[2 * i + 1].first;
		p[2 * i].second = i; p[2 * i + 1].second = i;
	}
	sort(p, p + 2 * N);
	int num[MAX_N];
	fill(num, num + N, 0);
	bool flag = true;
	int index = -1;
	for(int i = 0; i < N; i++){
		res3 += p[i].first;
		num[p[i].second]++;
		if(num[p[i].second] >= 2) flag = false;
	}
	if(flag){
		if(p[N - 1].second != p[N].second) res3 = res3 - p[N - 1].first + p[N].first;
		else res3 = res3 - max(p[N - 1].first - p[N + 1].first, p[N - 2].first - p[N].first);
	}
	printf("%lld\n", min(min(res1, res2), res3));
	return 0;
}

```