#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#include <time.h>
#include <queue>
#define int long long
using namespace std;
struct Vertex{int l; int r; int pr; int sz; int value; int sum;};
vector<Vertex> decart;
pair<int, int> SP = make_pair(-1, -1);
void update(int vertex){
    if (vertex == -1) return;
    int sz = 1, sum=decart[vertex].value;
    if (decart[vertex].l != -1){
        sz += decart[decart[vertex].l].sz;
        sum += decart[decart[vertex].l].sum;
    }
    if (decart[vertex].r != -1){
        sz += decart[decart[vertex].r].sz;
        sum += decart[decart[vertex].r].sum;
    }
    decart[vertex].sz = sz;
    decart[vertex].sum = sum;
}
pair<int, int> split(int father, int number){
    if (father == -1) return SP;
    int l = 1;
    if (decart[father].l != -1){
        l += decart[decart[father].l].sz;
    }
    if (l <= number){
        pair<int, int> p = split(decart[father].r, number - l);
        decart[father].r = p.first;
        p.first = father;
        update(father);
        return p;
    }
    pair<int, int> p = split(decart[father].l, number);
    decart[father].l = p.second;
    p.second = father;
    update(father);
    return p;
}
int merg(int first, int second){
    if (first == -1) return second;
    if (second == -1) return first;
    if (decart[first].pr >= decart[second].pr){
        int v = merg(decart[first].r, second);
        decart[first].r = v;
        update(first);
        return first;
    }
    int v = merg(first, decart[second].l);
    decart[second].l = v;
    update(second);
    return second;
}
int get_left(int base, int num){
    if (base==-1) return 0;
    if (decart[base].value >= num) return get_left(decart[base].l, num);
    int sz = 1;
    if (decart[base].l != -1) sz += decart[decart[base].l].sz;
    return get_left(decart[base].r, num) + sz;
}
int ins(int base, int num){
    int kek = get_left(base, num);
    pair<int, int> p = split(base, kek);
    Vertex nv = {-1, -1, rand(), 1, num,num};
    decart.push_back(nv);
    base = merg(p.first, decart.size()-1);
    base = merg(base, p.second);
    return base;
}
int ers(int base, int num){
    int kek = get_left(base, num);
    pair<int, int> p = split(base, kek);
    pair<int, int> p1 = split(p.second, 1);
    return merg(p.first, p1.second);
    //return base;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<int, int> > v;
    int base=-1;
    for (int i=0; i < n; i++){
        int a, b;
        cin >> a >> b;
        v.push_back(make_pair(a, b));
        base=ins(base, a);
        base=ins(base, b);
    }
    int ans = 1e18;
    for (int i=0; i < n; i++){
        base = ers(base, v[i].first);
        base = ers(base, v[i].second);
        pair<int, int> p = split(base, n-2);
        int ks = v[i].second+v[i].first;
        if (p.first != -1) ks += decart[p.first].sum;
        ans = min(ans, ks);
        base = merg(p.first, p.second);
        base = ins(base, v[i].first);
        base = ins(base,v[i].second);
    }
    int S = 0;
    for (int i=0; i < n; i++) S += v[i].first;
    ans = min(ans, S);
    S=0;
    for (int i=0; i < n; i++) S += v[i].second;
    ans = min(ans, S);
    cout << ans;
    return 0;
}




Measured memory_bytes: 30248