program min_cost_cycle
  implicit none
  integer :: n, a(100000), b(100000), c(200000,2), u(100000), i, k
  integer(8) :: y, z, xw, h, t
  a = 0
  b = 0
  c = 0
  u = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a(i), b(i)
    c(i,1) = a(i)
    c(i,2) = i
    c(n+i,1) = b(i)
    c(n+i,2) = -i
  end do
  y = sum(int(a(1:n),8))
  z = sum(int(b(1:n),8))
  call merge_sort(c(1:2*n,1:2))
  xw = sum(int(c(1:n,1),8))
  do i = 1, n
    k = abs(c(i,2))
    u(k) = u(k)+1
  end do
  if (maxval(u(1:n)).eq.1) then
    h = xw
    xw = 10000000000000000_8
    do i = 1, n
      if (c(i,2).eq.-c(n+1,2)) then
        t = h-c(i,1)+c(n+2,1)
      else
        t = h-c(i,1)+c(n+1,1)
      end if
      xw = min(xw,t)
    end do
  end if
  write(*,'(i0)') min(y,z,xw)
  stop
contains
  subroutine merge_sort(c)
    implicit none
    integer, intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(c1,c2)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,1).le.c2(i2,1)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merger
end program min_cost_cycle