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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static int N;
    static Node[] A, B;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();

        A = new Node[N];
        B = new Node[N];
        for (int i = 0; i < N; i++) {
            A[i] = new Node(i, sc.nextInt(), true);
            B[i] = new Node(i, sc.nextInt(), false);
        }

        System.out.println(solve());
    }

    static long solve() {
        Node[] all = new Node[N*2];
        System.arraycopy(A, 0, all, 0, N);
        System.arraycopy(B, 0, all, N, N);

        Arrays.sort(all, Comparator.comparingInt(n -> n.n));

        long half = 0;
        Set<Integer> found = new HashSet<>();
        int a = 0;
        int b = 0;
        boolean ab = false;
        for (int i = 0; i < N; i++) {
            Node node = all[i];
            half += node.n;
            if( node.a ) {
                a++;
            } else {
                b++;
            }
            if( found.contains(node.i) ) {
                ab = true;
            } else {
                found.add( node.i );
            }
        }
        if( a == 0 || b == 0 || ab ) {
            return half;

        } else {
            Node x1 = all[N-1];
            Node y1 = all[N];

            if( x1.i != y1.i ) {
                return half - x1.n + y1.n;
            } else {
                Node x2 = all[N-2];
                Node y2 = all[N+1];
                if( y1.n - x2.n < y2.n - x1.n ) {
                    return half - x2.n + y1.n;
                } else {
                    return half - x1.n + y2.n;
                }
            }
        }
    }

    static class Node {
        int i;
        int n;
        boolean a;

        public Node(int i, int n, boolean a) {
            this.i = i;
            this.n = n;
            this.a = a;
        }
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

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

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}
