#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int main() {
    ll n;
    cin >> n;
    ll a[n], b[n];
    rep(i,n)cin >> a[i] >> b[i];
    vector<P> v;
    rep(i,n){
        v.push_back(P(a[i], i));
        v.push_back(P(b[i], i));
    }
    sort(v.begin(), v.end());

    int f[n] = {};
    ll sum = 0;
    int ok = 0;
    rep(i,n){
        sum += v[i].first;
        f[v[i].second]++;
        if(f[v[i].second] == 2){
            ok = 1;
        }
    }


    // cout << v[n-1].first << "  " << v[n].first << endl;
    // cout << ok << endl;
    if(ok == 0){
        if(v[n-1].first == v[n+1].first)ok = 1;
        else if(v[n].first == v[n-1].first){
            if(v[n].second != v[n-1].second){
                ok = 1;
            }else if(v[n].first == v[n-2].first){
                ok = 1;
            }
        }

    }

    // cout << sum << endl;

    if(ok == 1){
        cout << sum << endl;
        return 0;
    }

    rep(i,n)f[i] = 0;
    v.clear();
    rep(i,n){
        v.push_back(P(a[i], i));
        v.push_back(P(b[i], i+n));
    }
    sort(v.begin(), v.end());
    rep(i,n){
        if(v[i].second < n) f[v[i].second]++;
        else f[v[i].second-n] += 10;
        if(f[v[i].second%n] == 11){
            ok = 1;
        }
    }

    sort(f, f+n);
    if(f[0] == f[n-1]){
        cout << sum << endl;
        return 0;
    }

    ll ans = 1001001001001001001;
    
    v.clear();
    rep(i,n){
        v.push_back(P(a[i], i));
        v.push_back(P(b[i], i+n));
    }
    sort(v.begin(), v.end());

    // rep(i,n)cout << v[i].first << ' ' << v[i].second << endl;

    vector<P> c, d, e, g;

    /*
    rep(i,n)cout << b[i] << endl;
    int ccc;
    cin >> ccc;
    */

    rep(i,n){
        if(v[i].second < n){
            c.push_back(P(v[i].first, v[i].second));
            d.push_back(P(b[v[i].second], v[i].second));
            if(b[v[i].second] > 1e9){
                // cout << b[v[i].second] << endl;
                // cout << v[i].second << endl;
                // int cc;cin >> cc;
            }
        }else{
            e.push_back(P(a[v[i].second-n], v[i].second-n));
            g.push_back(P(v[i].first, v[i].second-n));
        }
    }
    sort(c.begin(), c.end());
    sort(d.begin(), d.end());
    sort(e.begin(), e.end());
    sort(g.begin(), g.end());


    int m = c.size();
    int w = e.size();
    // cout << m << ' ' << w << endl;

    // rep(i,m)cout << c[i].first << ' ' << d[i].first << endl;
    // cout << v.size() << endl;
    int cc;
    // cin >> cc;

    if(m == 0 || w == 0){
        cout << sum << endl;
        return 0;
    }

    // cout << sum << endl;

    ll tmp = sum - g[w-1].first + d[0].first;
    // cout << g[w-1].first << ' ' << d[0].first << endl;
    ans = min(ans, tmp);
    // cout << tmp << endl;

    tmp = sum - c[m-1].first + e[0].first;
    ans = min(ans, tmp);
    // cout << tmp << endl;

    if(m == 1){
        tmp = sum - c[m-1].first + d[0].first;
        ans = min(ans, tmp);
    }else{
        tmp = sum - c[m-1].first + d[0].first;
        if(c[m-1].second != d[0].second){
            ans = min(ans, tmp);
        }else{
            tmp = sum - c[m-2].first + d[0].first;
            ans = min(ans, tmp);
            tmp = sum - c[m-1].first + d[1].first;
            ans = min(ans, tmp);
        }
    }
    // cout << tmp << endl;

    if(w == 1){
        tmp = sum - g[w-1].first + e[0].first;
        ans = min(ans, tmp);
    }else{
        tmp = sum - g[w-1].first + e[0].first;
        if(g[w-1].second != e[0].second){
            ans = min(ans, tmp);
        }else{
            tmp = sum - g[w-2].first + e[0].first;
            ans = min(ans, tmp);
            tmp = sum - g[w-1].first + e[1].first;
            ans = min(ans, tmp);
        }
    }
    // cout << tmp << endl;

    cout << ans << endl;
    return 0;
}




Measured memory_bytes: 13632