#include <iostream> 
#include <vector>
#include <map>
#include <set>
#include <algorithm> 
#include <string>

struct A
{
  A(int a = 0, int b = 0, int i = 0)
    : a(a), b(b), i(i)
  {}
  int a, b, i;
};

long long solve1(int n, std::vector < std::pair<int, int> > &v) {
  auto f1 = [](const A& x, const A &y) {
    if (x.a == y.a)
      return x.i < y.i;
    return x.a < y.a;
  };
  auto f2 = [](const A& x, const A &y) {
    if (x.b == y.b)
      return x.i > y.i;
    return x.b > y.b;
  };
  std::set<A, decltype(f1)> aa(f1);
  std::set<A, decltype(f2)> bb(f2);
  for (int i = 0; i < n; i++) {
    int a, b;
    a = v[i].first; b = v[i].second;
    A x(a, b, i);
    aa.insert(x);
    bb.insert(x);
  }
  long long sum = 0;
  int idxs = n;
  for (int i = 0; i < n - 1; i++) {
    auto ita = aa.begin();
    A x = *ita;
    auto itb = bb.begin();
    A y = *itb;
    if (x.i == y.i) {
      auto nita = ita;
      auto nitb = itb;
      nita++; nitb++;
      if (std::abs(nitb->b - ita->a) > std::abs(itb->b - nita->a)) {
        itb = nitb;
      }
      else {
        ita = nita;
      }
    }
    x = *ita;
    y = *itb;
    if (x.i != y.i) {
      aa.erase(ita);
      aa.erase(y);
      bb.erase(itb);
      bb.erase(x);
      sum += std::min(x.a, y.b);
      A xy = A(y.a, x.b, idxs++);
      aa.insert(xy);
      bb.insert(xy);
    }
    else {
      throw 1;
    }
  }
  return sum + std::min(aa.begin()->a, aa.begin()->b);
}

long long solve2(int n, std::vector<std::pair<int, int> > &v) {
  auto f1 = [](const A& x, const A &y) {
    if (x.a == y.a)
      return x.i < y.i;
    return x.a > y.a;
  };
  auto f2 = [](const A& x, const A &y) {
    if (x.b == y.b)
      return x.i > y.i;
    return x.b < y.b;
  };
  std::set<A, decltype(f1)> aa(f1);
  std::set<A, decltype(f2)> bb(f2);
  for (int i = 0; i < n; i++) {
    int a, b;
    a = v[i].first; b = v[i].second;
    A x(a, b, i);
    aa.insert(x);
    bb.insert(x);
  }
  long long sum = 0;
  int idxs = n;
  for (int i = 0; i < n - 1; i++) {
    auto ita = aa.begin();
    A x = *ita;
    auto itb = bb.begin();
    A y = *itb;
    if (x.i == y.i) {
      auto nita = ita;
      auto nitb = itb;
      nita++; nitb++;
      if (std::abs(nitb->b - ita->a) > std::abs(itb->b - nita->a)) {
        itb = nitb;
      }
      else {
        ita = nita;
      }
    }
    x = *ita;
    y = *itb;
    if (x.i != y.i) {
      aa.erase(ita);
      aa.erase(y);
      bb.erase(itb);
      bb.erase(x);
      sum += std::min(x.a, y.b);
      A xy = A(y.a, x.b, idxs++);
      aa.insert(xy);
      bb.insert(xy);
    }
    else {
      throw 1;
    }
  }
  return sum + std::min(aa.begin()->a, aa.begin()->b);
}

long long solve3(int n, std::vector < std::pair<int, int> > &v) {
  auto f1 = [](const A& x, const A &y) {
    if (x.a == y.a)
      return x.i < y.i;
    return x.a < y.a;
  };
  auto f2 = [](const A& x, const A &y) {
    if (x.b == y.b)
      return x.i > y.i;
    return x.b > y.b;
  };
  std::set<A, decltype(f1)> aa(f1);
  std::set<A, decltype(f2)> bb(f2);
  for (int i = 0; i < n; i++) {
    int a, b;
    a = v[i].first; b = v[i].second;
    A x(a, b, i);
    aa.insert(x);
    bb.insert(x);
  }
  long long sum = 0;
  int idxs = n;
  for (int i = 0; i < n - 1; i++) {
    auto ita = aa.begin();
    A x = *ita;
    auto itb = bb.begin();
    A y = *itb;
    if (x.i == y.i) {
      itb++;
    }
    x = *ita;
    y = *itb;
    if (x.i != y.i) {
      aa.erase(ita);
      aa.erase(y);
      bb.erase(itb);
      bb.erase(x);
      sum += std::min(x.a, y.b);
      A xy = A(y.a, x.b, idxs++);
      aa.insert(xy);
      bb.insert(xy);
    }
    else {
      throw 1;
    }
  }
  return sum + std::min(aa.begin()->a, aa.begin()->b);
}

long long solve4(int n, std::vector<std::pair<int, int> > &v) {
  auto f1 = [](const A& x, const A &y) {
    if (x.a == y.a)
      return x.i < y.i;
    return x.a > y.a;
  };
  auto f2 = [](const A& x, const A &y) {
    if (x.b == y.b)
      return x.i > y.i;
    return x.b < y.b;
  };
  std::set<A, decltype(f1)> aa(f1);
  std::set<A, decltype(f2)> bb(f2);
  for (int i = 0; i < n; i++) {
    int a, b;
    a = v[i].first; b = v[i].second;
    A x(a, b, i);
    aa.insert(x);
    bb.insert(x);
  }
  long long sum = 0;
  int idxs = n;
  for (int i = 0; i < n - 1; i++) {
    auto ita = aa.begin();
    A x = *ita;
    auto itb = bb.begin();
    A y = *itb;
    if (x.i == y.i) {
      itb++;
    }
    x = *ita;
    y = *itb;
    if (x.i != y.i) {
      aa.erase(ita);
      aa.erase(y);
      bb.erase(itb);
      bb.erase(x);
      sum += std::min(x.a, y.b);
      A xy = A(y.a, x.b, idxs++);
      aa.insert(xy);
      bb.insert(xy);
    }
    else {
      throw 1;
    }
  }
  return sum + std::min(aa.begin()->a, aa.begin()->b);
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  std::vector<std::pair<int, int> > v(n);
  for (int i = 0; i < n; i++) {
    std::cin >> v[i].first >> v[i].second;
  }
  std::vector<long long> res;
  res.push_back(solve1(n, v));
  res.push_back(solve2(n, v));
  res.push_back(solve3(n, v));
  res.push_back(solve4(n, v));
  std::sort(res.begin(), res.end());
  std::cout << res[0] << std::endl;
}

Measured memory_bytes: 13568