// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// }}}
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <stack>
#include <tuple>
#include <vector>
// clang-format off
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":", ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream &o, tuple<T...> const &t){ o << "("; _ot<0>(o, t); o << ")"; return o; }
template<class T, class U> ostream & operator<<(ostream &o, pair<T, U> const &p) { o << "(" << p.first << ", " << p.second << ")"; return o; }
template < class T > ostream &operator<<(ostream &o, const stack<T> &a) { o << "{"; for(auto tmp = a; tmp.size(); tmp.pop()) o << (a.size() == tmp.size() ? "" : ", ") << tmp.top(); o << "}"; return o; }
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT cerr
#endif
#define dump(...) [&](){auto __debug_tap=make_tuple(__VA_ARGS__);DEBUG_OUT<<"["<<__LINE__<< "] "<<#__VA_ARGS__<<" = "<<__debug_tap<<"\n";}()
template < class T > inline void dump2D(T &d, size_t sizey, size_t sizex) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for(size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << endl; } }
template < class T, class = typename iterator_traits< typename T::iterator >::value_type, class = typename enable_if<!is_same<T, string>::value>::type > ostream &operator<<(ostream &o, const T &a) { o << "{"; for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : ", ") << *ite; o << "}"; return o; }
#else
#define dump(...) (42)
#define dump2D(...) (42)
template < class T, class = typename iterator_traits< typename T::iterator >::value_type, class = typename enable_if<!is_same<T, string>::value>::type > ostream &operator<<(ostream &o, const T &a) { for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : " ") << *ite; return o; }
#endif
// clang-format on
// }}}


const int N = 1e5;
int a[N], b[N];
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n;
  cin >> n;
  int used[2][N] = {};
  vector<tuple<int, int, int>> v;
  for(int i = 0; i < n; i++) cin >> a[i] >> b[i], v.emplace_back(a[i], 0, i), v.emplace_back(b[i], 1, i);
  sort((v).begin(), (v).end());
  int x = 0, y = 0, z = 0, w = 0;
  ll ans = 0;
  for(int i = 0; i < n; i++) {
    int x, t, idx;
    tie(x, t, idx) = v[i];
    used[t][idx] = 1;
    ans += x;
  }
  ll goX0 = -1, goX1 = -1, x0, x1;
  ll goW0 = -1, goW1 = -1, w0, w1;
  for(int i = 0; i < n; i++) {
    if(used[0][i] && used[1][i]) x++;
    else if(used[0][i] && !used[1][i]) {
      y++;
      if(goX0 == -1 || (x0 > b[i])) goX1 = goX0, x1 = x0, goX0 = i, x0 = b[i];
      else if(goX1 == -1 || (x1 > b[i])) goX1 = i, x1 = b[i];
      if(goW0 == -1 || (w0 < a[i])) goW1 = goW0, w1 = w0, goW0 = i, w0 = a[i];
      else if(goW1 == -1 || (w1 < a[i])) goW1 = i, w1 = a[i];
    }
    else if(!used[0][i] && used[1][i]) {
      z++;
      if(goX0 == -1 || (x0 > a[i])) goX1 = goX0, x1 = x0, goX0 = i, x0 = a[i];
      else if(goX1 == -1 || (x1 > a[i])) goX1 = i, x1 = a[i];
      if(goW0 == -1 || (w0 < b[i])) goW1 = goW0, w1 = w0, goW0 = i, w0 = b[i];
      else if(goW1 == -1 || (w1 < b[i])) goW1 = i, w1 = b[i];
    }
    else if(!used[0][i] && !used[1][i]) w++;
  }
  if(y && z && !x && !w) {
    if(goX0 != goW0) {
      ans += x0 - w0;
    } else {
      if(x0 - w1 > x1 - w0) {
        ans += x1 - w0;
      } else{
        ans += x0 - w1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 6260