#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)
#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
	return os;
}

typedef long long weight;
typedef pair<weight, int> P;

struct edge {
	int to, cap, rev;
	weight cost;
	edge(int t, int ca, weight co, int r):to(t), cap(ca), cost(co), rev(r){};
};

vector<vector<edge> > G;

void init(int n) {
	G.clear();
	G.resize(n);
}

void add_edge(int from, int to, int cap, weight cost) {
	G[from].push_back(edge(to, cap, cost, G[to].size()));
	G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}

weight min_cost_flow(int s, int t, int f) {
	const int n = G.size();
	weight res = 0;
	vector<int> prevv(n), preve(n);
	vector<weight> h(n, 0);
	while(f > 0) {
		priority_queue<P, vector<P>, greater<P> > que;
		vector<weight> dist(n, LLONG_MAX);
		dist[s] = 0;
		que.push(P(0, s));
		while(!que.empty()) {
			const P p = que.top();
			que.pop();
			const int v = p.second;
			if(dist[v] < p.first)
				continue;

			for(int i = 0; i < G[v].size(); ++i) {
				const edge& e = G[v][i];
				if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
					dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					que.push(P(dist[e.to], e.to));
				}
			}
		}

		if(dist[t] == INT_MAX)
			return res;

		for(int v = 0; v < n; ++v)
			h[v] += dist[v];

		int d = f;
		for(int v = t; v != s; v = prevv[v])
			d = min(d, G[prevv[v]][preve[v]].cap);

		if(h[t] >= 0)
			return res;

		f -= d;
		res += d * h[t];
		for(int v = t; v != s; v = prevv[v]) {
			edge& e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}

	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vector<int> a(n), b(n), len_list;;
	len_list.reserve(2 * n);

	for(int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
		len_list.push_back(a[i]);
		len_list.push_back(b[i]);
	}

	sort(len_list.begin(), len_list.end());
	len_list.erase(unique(len_list.begin(), len_list.end()), len_list.end());

	init(3 * n + len_list.size() + 2);
	const int source = 3 * n + len_list.size();
	const int sink = source + 1;

	for(int i = 0; i < n; ++i) {
		add_edge(source, i, 1, 0);

		const int a_idx = lower_bound(len_list.begin(), len_list.end(), a[i]) - len_list.begin();
		add_edge(i, n + i, 1, 0);
		add_edge(n + i, 3 * n + a_idx, 1, -b[i]);

		const int b_idx = lower_bound(len_list.begin(), len_list.end(), b[i]) - len_list.begin();
		add_edge(i, 2 * n + i, 1, 0);
		add_edge(2 * n + i, 3 * n + b_idx, 1, -a[i]);
	}

	for(int i = 0; i < (int)len_list.size(); ++i) {
		add_edge(3 * n + i, sink, 1, 0);
	}

	cout << -min_cost_flow(source, sink, n) << endl;

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1956