/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#include<vector>

class BitSum {
    vector<long long> bit;
    int MAXN;

    // return bit[1] + ... + bit[i]
public: long long sum(int i) {
      long long s = 0;
      while (i > 0) {
        s += bit[i];
        i -= i & -i;
      }
      return s;
    }

    // bit[i] += x
    BitSum(int n) {
      MAXN = n;
      bit.resize(n+1);
    }

public: void add(int i, long long x) {
      while (i <= MAXN) {
        bit[i] += x;
        i += i & -i;
      }
    }

public: void reset() {
      for(int i=0; i<=MAXN; i++) {
        bit.at(i) = 0;
      }
    }

    // return the smallest i where sum(i) >= val
    // If sum(MAXN) < val, return INT_MAX
public: int lower_bound(long long val) {
      int min = 0;
      int max = MAXN;
      if (sum(max) < val) return INT_MAX;
      if (sum(0) >= val) return 0;
      while(min + 1 < max) {
        int now = (min+max)/2;
        if (sum(now) >= val) {
          max = now;
        } else {
          min = now;
        }
      }
      return max;
    }
};


class CMinCostCycle {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      vector<ll> a(n), b(n);
      rep(i, n) {
        cin >> a[i] >> b[i];
      }

      vector<P> v;
      rep(i, n) {
        v.emplace_back(a[i], i);
        v.emplace_back(b[i], i+n);
      }
      sort(all(v));

      bool usedA = false;
      bool usedB = false;
      set<int> remaining;
      rep(i, n) {
        remaining.insert(i);
      }

      ll ret = 0;
      rep(i, n-1) {
        ret += v[i].first;
        if (v[i].second >= n) {
          usedB = true;
          v[i].second -= n;
        } else {
          usedA = true;
        }
        remaining.erase(v[i].second);
      }

      int idx = n-1;
      if (remaining.size() == 1) {
        while (remaining.find(v[idx].second%n) != remaining.end()) {
          if (!usedA && v[idx].second >= n) break;
          if (!usedB && v[idx].second < n) break;
          idx++;
        }
      }
      ret += v[idx].first;

      if (idx == n+1) {
        chmin(ret, ret-v[idx].first+v[idx-1].first+v[idx-2].first-v[idx-3].first);
      }



      cout << ret << endl;
    }
};

signed main() {
  CMinCostCycle solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 12644