import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList paths = new ArrayList<>(); long total = 0; for (int i = 1; i <= n; i++) { String[] line = br.readLine().split(" ", 2); int c = Integer.parseInt(line[0]); int d = Integer.parseInt(line[1]); total += c; paths.add(new Path(c, 0, i)); if (i > 1) { paths.add(new Path(d, i - 1, i)); } } Collections.sort(paths); UnionFindTree uft = new UnionFindTree(n + 1); for (Path p : paths) { if (!uft.same(p)) { uft.unite(p); total += p.value; } } System.out.println(total); } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public boolean same(Path p) { return same(p.left, p.right); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(y)] = find(x); } } public void unite(Path p) { unite(p.left, p.right); } } static class Path implements Comparable { int value; int left; int right; public Path(int value, int left, int right) { this.value = value; this.left = left; this.right = right; } public int compareTo(Path another) { return value - another.value; } } } class BinaryIndexedTree { int size; int[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new int[size]; } public void add(int idx, int value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; idx += mask; } mask <<= 1; } } public int getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public int getSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } }