#define ENABLE_LL
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try { 

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " " 
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++) 
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 100005;
int a[N][2];
int n;
vector < prr > s;

void init() {
    scanf(I,&n);
    rep (i,n) scanf(II,&a[i][0],&a[i][1]);
    rep (i,n) {
        s.push_back( prr(pr(a[i][0],i),0) );
        s.push_back( prr(pr(a[i][1],i),1) );
    }
    sort(s.begin(), s.end());
}

int cho[N][2];
bool ok (vector < pr > a) {
    memset(cho,0,sizeof(cho));
    for (auto i:a) cho[i.L][i.R] ++;
    int s[2][2]; memset(s,0,sizeof(s));
    rep (i,n) s[cho[i][0]][cho[i][1]]++;
    ses(s[0][0]);ses(s[0][1]);ses(s[1][0]);see(s[1][1]);
    if (s[0][0] != s[1][1]) return 0;
    if (s[0][0] == 0 && s[0][1] && s[1][0]) return 0;
    return 1;
}

void solve() {
    int s1=0; rep (i,n-2) s1+=s[i].L.L; vector < pr > c1; rep (i,n-2) c1.push_back(pr(s[i].L.R,s[i].R));
    int s2=0; rep (i,n-2) s2+=s[i].L.L; vector < pr > c2; rep (i,n-2) c2.push_back(pr(s[i].L.R,s[i].R));
    int s3=0; rep (i,n-2) s3+=s[i].L.L; vector < pr > c3; rep (i,n-2) c3.push_back(pr(s[i].L.R,s[i].R));
    int s4=0; rep (i,n-2) s4+=s[i].L.L; vector < pr > c4; rep (i,n-2) c4.push_back(pr(s[i].L.R,s[i].R));
    s1 += s[n-2].L.L; s1 += s[n-1].L.L; c1.push_back(pr(s[n-2].L.R,s[n-2].R)); c1.push_back(pr(s[n-1].L.R,s[n-1].R));
    s2 += s[n-2].L.L; s2 += s[n  ].L.L; c2.push_back(pr(s[n-2].L.R,s[n-2].R)); c2.push_back(pr(s[n  ].L.R,s[n  ].R));
    s3 += s[n-1].L.L; s3 += s[n  ].L.L; c3.push_back(pr(s[n-1].L.R,s[n-1].R)); c3.push_back(pr(s[n  ].L.R,s[n  ].R));
    s4 += s[n-2].L.L; s4 += s[n+1].L.L; c4.push_back(pr(s[n-2].L.R,s[n-2].R)); c4.push_back(pr(s[n+1].L.R,s[n+1].R));
    int ans = 0x3f3f3f3f3f3f3f3f;
    if (ok(c1)) checkMin(ans, s1);
    if (ok(c2)) checkMin(ans, s2);
    if (ok(c3)) checkMin(ans, s3);
    if (ok(c4)) checkMin(ans, s4);
    printf(IN,ans);
}


Measured memory_bytes: 19460