We have a directed weighted graph with N vertices. Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i.

In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N, and its weight is {\rm min}(A_x,B_y).

We will consider a directed cycle in this graph that visits every vertex exactly once. Find the minimum total weight of the edges in such a cycle.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
* 1 \leq B_i \leq 10^9
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 B_1
A_2 B_2
:
A_N B_N


Output

Print the minimum total weight of the edges in such a cycle.

Examples

Input

3
1 5
4 2
6 3


Output

7


Input

4
1 5
2 6
3 7
4 8


Output

10


Input

6
19 92
64 64
78 48
57 33
73 6
95 73


Output

227
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        LightScanner in = new LightScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CMinCostCycle solver = new CMinCostCycle();
        solver.solve(1, in, out);
        out.close();
    }

    static class CMinCostCycle {
        public void solve(int testNumber, LightScanner in, PrintWriter out) {
            int n = in.ints();
            List<CMinCostCycle.Node> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(new CMinCostCycle.Node(in.ints(), true, i));
                a.add(new CMinCostCycle.Node(in.ints(), false, i));
            }
            a.sort(Comparator.comparing(node -> node.cost));
            int[] use = new int[n];
            long ans = 0;
            boolean du = false;
            int type = 0;
            for (int i = 0; i < n; i++) {
                int index = a.get(i).i;
                ans += a.get(i).cost;
                if (use[index] > 0) {
                    du = true;
                }
                if (a.get(i).in) {
                    type |= 1;
                } else {
                    type |= 2;
                }
                use[index]++;
            }
            if (!du && type == 3) {
                int d;
                if (a.get(n - 1).i == a.get(n).i) {
                    d = a.get(n + 1).cost - a.get(n - 1).cost;
                } else {
                    d = a.get(n).cost - a.get(n - 1).cost;
                }
                if (a.get(n - 2).i == a.get(n).i) {
                    d = Math.min(d, a.get(n + 1).cost - a.get(n - 2).cost);
                } else {
                    d = Math.min(d, a.get(n).cost - a.get(n - 2).cost);
                }
                ans += d;
            }
            out.println(ans);
        }

        static class Node {
            int cost;
            boolean in;
            int i;

            public Node(int cost, boolean in, int i) {
                this.cost = cost;
                this.in = in;
                this.i = i;
            }

        }

    }

    static class LightScanner {
        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public LightScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        public String string() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int ints() {
            return Integer.parseInt(string());
        }

    }
}

