#include <bits/stdc++.h>

using namespace std;

const int max_n = 100111, inf = 1000111222;

int n, a[max_n], b[max_n];
multiset<pair<int, int>> xy, yx;

void add(int x, int y) {
    //cout << "+ " << x << " " << y << endl;
    xy.insert({x, y});
    yx.insert({y, x});
}

void del(int x, int y) {
    //cout << "- " << x << " " << y << endl;
    xy.erase(xy.find({x, y}));
    yx.erase(yx.find({y, x}));
}

vector<int> all;
int f[max_n][2];
vector<pair<int, pair<int, int>>> v;

bool B[4][4];

bool bad(int a, int b, int c, int d) {
    vector<int> q;
    while (a--) {
        q.push_back(0);
    }
    while (b--) {
        q.push_back(1);
    }
    while (c--) {
        q.push_back(2);
    }
    while (d--) {
        q.push_back(3);
    }
    do {
        bool ok = true;
        for (int i = 0; i < q.size(); ++i) {
            if (B[q[i]][q[(i + 1) % q.size()]]) {
                ok = false;
                break;
            }
        }
        if (ok) {
            return false;
        }
    } while (next_permutation(q.begin(), q.end()));
    return true;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    /*B[0][0] = 1;
    B[0][2] = 1;
    B[1][0] = 1;
    B[1][2] = 1;
    for (int a = 0; a < 5; ++a) {
        for (int b = 0; b < 5; ++b) {
            for (int c = 0; c < 5; ++c) {
                for (int d = 0; d < 5; ++d) {
                    if (2 * a + b + c == a + b + c + d && bad(a, b, c, d)) {
                        cout << a << " " << b << " " << c << " " << d << endl;
                    }
                }
            }
        }
    }
    return 0;*/
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &a[i], &b[i]);
        add(a[i], b[i]);
        all.push_back(a[i]);
        all.push_back(b[i]);
    }
    sort(all.begin(), all.end());
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += all[i];
    }
    for (int i = 0; i < n; ++i) {
        v.push_back(make_pair(a[i], make_pair(i, 0)));
        v.push_back(make_pair(b[i], make_pair(i, 1)));
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < n; ++i) {
        f[v[i].second.first][v[i].second.second] = 1;
    }
    int a = 0, b = 0, c = 0, d = 0;
    for (int i = 0; i < n; ++i) {
        if (f[i][0] && f[i][1]) {
            ++a;
        } else if (f[i][0]) {
            ++b;
        } else if (f[i][1]) {
            ++c;
        } else {
            ++d;
        }
    }
    assert(a == d);
    if (a == 0 && b > 0 && c > 0) {
        ans -= all[n - 1];
        ans += all[n];
        if (v[n - 1].second.first == v[n].second.first) {
            if (v[n].second.second == 0) {
                --c;
                ++b;
            } else {
                ++c;
                --b;
            }
            if (b > 0 && c > 0) {
                ans -= all[n];
                ans -= all[n - 2];
                ans = min(ans + all[n - 2] + all[n + 1], ans + all[n - 1] + all[n]);
            }
        }
    }
    cout << ans << "\n";
    /*long long ans = 0;
    while (xy.size() > 1) {
        pair<int, int> p1 = *xy.begin();
        pair<int, int> p2 = *yx.begin();
        int x, y, tp;
        if (p1.first < p2.first) {
            x = p1.first;
            y = p1.second;
            tp = 0;
        } else {
            x = p2.second;
            y = p2.first;
            tp = 1;
        }
        ans += min(x, y);
        //cout << x << " " << y << endl;
        del(x, y);
        if (tp == 0) {
            pair<int, int> p = *yx.rbegin();
            del(p.second, p.first);
            add(p.second, y);
        } else {
            pair<int, int> p = *xy.rbegin();
            del(p.first, p.second);
            add(x, p.second);
        }
    }
    ans += min(xy.begin()->first, xy.begin()->second);
    if (ans != qqq) {
        qqq -= all[n - 1];
        qqq += all[n];
        ans = qqq;
    }
    cout << ans << "\n";*/
    return 0;
}


Measured memory_bytes: 16240