import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val A = new Array[Int](N+1)
  val B = new Array[Int](N+1)
  for (i <- 1 to N) {
    val a, b = in.next().toInt
    A(i) = a
    B(i) = b
  }


  // check smallest
  val hoge = new Array[(Long, Int)](2*N)
  for (i <- 1 to N) {
    val ind = 2*(i-1)
    hoge(ind) = (A(i), -i)
    hoge(ind+1) = (B(i), i)
  }

  val ord = hoge.sorted
  var ans = ord.take(N).map(_._1).sum
  var flag1 = false
  val memo = new Array[Boolean](N+1)
  for (i <- 0 until N) {
    val (_, _ind) = ord(i)
    val ind = _ind.abs

    if(memo(ind))
      flag1 = true
    else
      memo(ind) = true
  }

  if(A.sum == ans || B.sum == ans || flag1)
    ()
  else {
    val dpA = new Array[Boolean](N+1)
    val dpB = new Array[Boolean](N+1)

    for (i <- 0 until N) {
      val (_, ind) = ord(i)

      if(ind < 0)
        dpA(ind.abs) = true
      else
        dpB(ind.abs) = true
    }

    dpA(-1)
  }


  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}

