#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
//#define int long long

int n;
long long ans = 1e18;
pair<int, int> x[500000];

long long summ[500000];
vector<long long> g;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i].first >> x[i].second;
    vector<int> mi, mx;
    long long si = 0;
    for (int i = 0; i < n; i++) si += x[i].first; ans = min(ans, si); si = 0;
    for (int i = 0; i < n; i++) si += x[i].second; ans = min(ans, si); si = 0;
    for (int i = 0; i < n; i++) g.push_back(x[i].first), g.push_back(x[i].second);
    sort(all(g));
    long long s = 0;
    map<int, int> cnt;
    for (int i = 0; i < n - 2; i++) s += g[i], cnt[g[i]]++;
    for (int i = 0; i < n; i++)
    {
        int a = min(x[i].fi, x[i].se);
        int b = max(x[i].fi, x[i].se);
        int ep = n - 2;
        int ma = 0, mb = 0;
        if (cnt.find(a) != cnt.end())
        {
            cnt[a]--;
            s -= a;
            if (cnt[a] == 0) cnt.erase(a);
            cnt[g[ep]]++;
            s += g[ep];
            ep++;
            ma = 1;
        }
        if (cnt.find(b) != cnt.end())
        {
            cnt[b]--;
            s -= b;
            if (cnt[b] == 0) cnt.erase(b);
            cnt[g[ep]]++;
            s += g[ep];
            ep++;
            mb = 1;
        }
        ans = min(ans, s + a + b);
        if (mb)
        {
            mb = 0;
            ep--;
            s -= g[ep];
            cnt[g[ep]]--;
            if (cnt[g[ep]] == 0) cnt.erase(g[ep]);
            s += b;
            cnt[b]++;
        }
        if (ma)
        {
            ma = 0;
            ep--;
            s -= g[ep];
            cnt[g[ep]]--;
            if (cnt[g[ep]] == 0) cnt.erase(g[ep]);
            s += a;
            cnt[a]++;
        }
    }
    cout << ans;
}


Measured memory_bytes: 11120