// Arup Guha
// 5/9/2017
// Solution to 2016 NCPC Problem H: Highest Tower

import java.util.*;
import java.io.*;

public class h {

	public static int n;
	public static int[][] list;
	public static int v;
	public static ArrayList[] graph;
	public static HashMap<Integer,Integer> map;
	public static int[] vals;
	public static int[] compNum;

	public static void main(String[] Args) throws Exception {

		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(stdin.readLine().trim());
		list = new int[n][2];
		TreeSet<Integer> ts = new TreeSet<Integer>();

		// Read in edges and store unique values.
		for (int i=0; i<n; i++) {
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			list[i][0] = Integer.parseInt(tok.nextToken());
			list[i][1] = Integer.parseInt(tok.nextToken());
			ts.add(list[i][0]);
			ts.add(list[i][1]);
		}

		// Store mapping between integers and graph vertex numbers.
		v = ts.size();
		vals = new int[v];
			map = new HashMap<Integer,Integer>();
		for (int i=0; i<v; i++) {
			vals[i] = ts.pollFirst();
			map.put(vals[i], i);
		}

		// Set up graph.
		graph = new ArrayList[v];
		for (int i=0; i<v; i++) graph[i] = new ArrayList<Integer>();
		for (int i=0; i<n; i++) {
			int v1 = map.get(list[i][0]);
			int v2 = map.get(list[i][1]);
			graph[v1].add(v2);
			graph[v2].add(v1);
		}

		// Will store the component number of each vertex.
		compNum = new int[v];
		Arrays.fill(compNum, -1);
		int cur = 0;

		// Fill each unfilled component.
		for (int i=0; i<v; i++)
			if (compNum[i] == -1)
				dfs(i, cur++);

		// Stores # vertices, #edges and max for each component.
		int[] vCount = new int[cur];
		int[] eCount = new int[cur];
		int[] max = new int[cur];

		long res = 0;

		// Go through each vertex, adding required heights, and also counting up the # of vertices and edges in each component.
		for (int i=0; i<v; i++) {
			vCount[compNum[i]]++;
			eCount[compNum[i]] += graph[i].size();
			res += (graph[i].size()-1)*((long)vals[i]);
			max[compNum[i]] = Math.max(max[compNum[i]], vals[i]);
		}

		// We can add in the maximum of each component that doesn't have a cycle.
		for (int i=0; i<cur; i++)
			if (eCount[i]/2 < vCount[i])
				res += max[i];

		// Output the result.
		System.out.println(res);
	}

	// Run a DFS, marking ID for each vertex in this component.
	public static void dfs(int v, int ID) {
		compNum[v] = ID;
		for (Integer x : (ArrayList<Integer>)graph[v])
			if (compNum[x] < 0)
				dfs(x, ID);
	}
}