Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well.

Today's training is to gain dexterity that never mistypes by carefully stacking blocks. Since there are many building blocks, let's build a tall tower.

There are N blocks, and the i-th (1 ≤ i ≤ N) building blocks are in the shape of a rectangular parallelepiped of 1 x Ai x Bi. The side of length 1 is used in the depth direction, and the sides of lengths Ai and Bi are assigned one by one in the horizontal direction and one in the height direction. When building blocks, the upper building blocks must be exactly shorter in width than the lower building blocks. The blocks can be used in any order, and some blocks may not be used. Under these restrictions, I want to make the tallest tower that can be built.



Input


N
A1 B1
...
AN BN


Satisfy 1 ≤ N ≤ 1,000, 1 ≤ Ai, Bi ≤ 1,000,000. All input values ​​are integers.

Output

Output the maximum height of the tower on one line.

Examples

Input

3
10 40
10 40
20 30


Output

80


Input

4
1 2
2 3
3 4
4 1


Output

11
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>
#include <complex>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef complex<double> P;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define MOD 1000000007

//AOJ 2293

struct edge{
	int to, cost, cap, rev;
	edge(int _to, int _cost, int _cap, int _ret):to(_to),cost(_cost),cap(_cap),rev(_ret){};
};

int n,a[1111],b[1111];
int inf = 1111111111;
vector<vector<edge> > g;
set<int> si;
int nid[1111111];
int dist[3333],nv,pe[3333],pv[3333];
 
void addedge(int from, int to, int cost, int cap){
	g[from].PB(edge(to, cost, cap, g[to].size()));
	g[to].PB(edge(from,-cost, 0, g[from].size()-1));
}

inline void shortest_path(int s){
	int flag = true;
	REP(i,nv)dist[i]=inf;
	dist[s]=0;
	while(flag){
		flag = false;
		REP(i,nv){
			REP(j,g[i].size()){
				if(g[i][j].cap > 0 && dist[g[i][j].to]>dist[i]+g[i][j].cost){
					dist[g[i][j].to]=dist[i]+g[i][j].cost;
					pe[g[i][j].to]=j;
					pv[g[i][j].to]=i;
					flag = true;
				}
			}
		}
	}
}

int MinCostFlow(int s, int t, int f){
	int ret = 0;
	while(f){
		shortest_path(s);
		if(dist[t]==inf)return inf;
		int d=f, c=t;
		while(c!=s){
			d=min(d, g[pv[c]][pe[c]].cap);
			c=pv[c];
		}
		f-=d;
		ret+=d*dist[t];
		c=t;
		while(c!=s){
			g[pv[c]][pe[c]].cap-=d;
			g[c][g[pv[c]][pe[c]].rev].cap+=d;
			c=pv[c];
		}
	}
	return ret;
}

int main(){
	cin>>n;
	REP(i,n){
		scanf("%d%d", &a[i], &b[i]);
		si.insert(a[i]);
		si.insert(b[i]);
	}
	nv=si.size()+n+3;
	int k=n+1;
	memset(nid, -1, sizeof(nid));
	g=vector<vector<edge> >(nv);
	REP(i,n){
		int va,vb;
		addedge(0,i+1,0,1);
		if(nid[a[i]]==-1)nid[a[i]]=k++;
		if(nid[b[i]]==-1)nid[b[i]]=k++;
		addedge(i+1,nid[a[i]],-b[i],1);
		addedge(i+1,nid[b[i]],-a[i],1);
		addedge(i+1,nv-2,0,1);
	}
	FOR(i,n+1,nv-2)addedge(i,nv-1,0,1);
	addedge(nv-2,nv-1,0,inf);
	cout << -MinCostFlow(0,nv-1,n) << endl;
}