#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 500050;
const int K = 20;
int s[N], t[N], f[N], s1[N], s2[N], ans;
pair < int, int > a[N], b[N];
int sum (int r)
{
	int result = 0;
	for (; r >= 0; r = (r & (r+1)) - 1)
		result += t[r];
	return result;
}

void inc (int i, int delta)
{
	for (; i < N; i = (i | (i+1)))
		t[i] += delta;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
   // freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    int res = 0;
    for (int i = 1; i <= n; i++){
        cin >> a[i].F >> b[i].F;
        a[i].S = i;
        b[i].S = i;
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    reverse(b + 1, b + n + 1);
    for (int i = 1; i <= n; i++){
        f[a[i].S] = i;
        a[i].S = i;
    }
    for (int i = 1; i <= n; i++){
        b[i].S = f[b[i].S];
      //  cout << a[i].F << " " << b[i].F << endl;
        s1[i] = s1[i - 1] + a[i].F;
        s2[i] = s2[i - 1] + b[i].F;
    }
    int ans =1e17;
    for (int i = 1; i <= n; i++){
        inc(b[i].S, 1);
        if (a[i].F > b[i].F) break;
        if (sum(a[i].S) == a[i].S){
                if ((a[i + 1].S == b[i + 1].S && i + 1 != n) && (a[i].S == b[i].S && i != 1)){
                    if (i + 2 <= n && i > 1) ans = min(ans, s[n] - s[i + 2] + s1[i - 2] + min(a[i].F, b[i + 2].F) + min(a[i + 1].F, b[i - 1].F) + a[i - 1].F + b[i + 1].F);
                    if (i + 2 <= n && i > 1) ans = min(ans, s[n] - s[i + 2] + s1[i - 2] + min(a[i].F, b[i + 1].F) + min(a[i + 2].F, b[i - 1].F) + a[i - 1].F + b[i + 2].F);
                    if (i + 2 <= n && i > 1) ans = min(ans, s[n] - s[i + 2] + s1[i - 2] + min(b[i].F, a[i + 2].F) + min(b[i + 1].F, a[i - 1].F) + a[i].F + b[i + 2].F);
                    if (i + 2 <= n && i > 1) ans = min(ans, s[n] - s[i + 2] + s1[i - 2] + min(b[i].F, a[i + 1].F) + min(b[i + 2].F, a[i - 1].F) + a[i].F + b[i + 1].F);
                } else
                if ((a[i + 1].S == b[i + 1].S && i + 1 != n)){
                    if (i + 2 <= n) ans = min(ans, s1[i - 1] + s2[n] - s2[i + 2] + min(a[i].F, b[i + 2].F) + min(a[i + 1].F, b[i].F) + b[i + 1].F);
                    if (i + 2 <= n) ans = min(ans, s1[i - 1] + s2[n] - s2[i + 1] + min(a[i].F, b[i + 1].F) + min(a[i + 2].F, b[i].F));
                } else
                if ((a[i].S == b[i].S && i != 1)){
                    if (i > 1) ans = min(ans, s1[i - 2] + s2[n] - s2[i + 1] + min(a[i].F, b[i + 1].F) + min(b[i - 1].F, a[i + 1].F) + a[i - 1].F);
                    if (i > 1) ans = min(ans, s1[i - 2] + s2[n] - s2[i + 1] + min(a[i - 1].F, b[i + 1].F) + min(b[i].F, a[i + 1].F) + a[i].F);
                } else{
                    if (i < n) ans = min(ans, s1[i - 1] + s2[n] - s2[i + 1] + min(a[i].F, b[i + 1].F) + min(a[i + 1].F, b[i].F));
                }

          // cout << ans << endl;
        } else{
            if (sum(a[i].S) != a[i].S)
            ans = min(ans, s1[i] + s2[n] - s2[i]);
        }
    }
    res = 0;
    for (int i = 1; i <= n; i++) res += min(a[i].F, b[i].F);
    int cnt = 0;
    for (int i = 1; i <= n; i++){
        if (a[i].F == b[i].F){
            cnt++;
        }
    }
    if (cnt > 1){
        cout << res;
        return 0;
    }
    if (a[1].F > b[1].F){
        ans = min(ans, s2[n]);
    }
    if (a[n].F < b[n].F){
        ans = min(ans, s1[n]);
    }
    cout << ans;
}


Measured memory_bytes: 18176