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 <bits/stdc++.h>
using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct Edge{
	int src,dst,cap,cost;
	Edge(){}
	Edge(int s,int d,int ca,int co):src(s),dst(d),cap(ca),cost(co){}
};
typedef vector<vector<Edge> > Graph;

int MinCostFlow(const Graph& _g,int begin,int end,int flow)
{
	int size=_g.size();
	Graph g(size);
	vvi rev(size);
	rep(i,size) for(auto e:_g[i]){
		g[i].push_back(e);
		g[e.dst].push_back(Edge(e.dst,e.src,0,-e.cost));
		rev[i].push_back(g[e.dst].size()-1);
		rev[e.dst].push_back(g[i].size()-1);
	}
	
	int res=0;
	vi pots(size);
	while(flow){
		vi dist(size,INF);
		vector<pii> prev(size,mp(-1,-1));
		priority_queue<pii,vector<pii>,greater<pii> > pq;
		pq.push(mp(0,begin));
		dist[begin]=0;
		while(!pq.empty()){
			int curd=pq.top().first,curv=pq.top().second; pq.pop();
			if(dist[curv]<curd)
				continue;
			rep(i,g[curv].size()){
				Edge e=g[curv][i];
				if(e.cap==0)
					continue;
				int newdist=dist[e.src]+e.cost+pots[e.src]-pots[e.dst];
				if(dist[e.dst]>newdist){
					dist[e.dst]=newdist;
					prev[e.dst]=mp(e.src,i);
					pq.push(mp(newdist,e.dst));
				}
			}
		}
		
		if(prev[end].first==-1)
			return -1;
		rep(i,size)
			pots[i]+=dist[i];
		
		int f=flow;
		for(int v=end;v!=begin;v=prev[v].first){
			Edge e=g[prev[v].first][prev[v].second];
			f=min(f,e.cap);
		}
		for(int v=end;v!=begin;v=prev[v].first){
			int i=prev[v].first,j=prev[v].second;
			g[i][j].cap-=f;
			g[g[i][j].dst][rev[i][j]].cap+=f;
		}
		flow-=f;
		res+=f*pots[end];
	}
	
	return res;
}

int main()
{
	for(int n;cin>>n && n;){
		vi as(n),bs(n);
		rep(i,n) cin>>as[i]>>bs[i];
		map<int,int> f;
		rep(i,n) f.insert(mp(as[i],f.size()));
		rep(i,n) f.insert(mp(bs[i],f.size()));
		int m=f.size();
		Graph g(n+m+2);
		int tap=n+m,sink=tap+1;
		rep(i,n){
			g[tap].emplace_back(tap,i,1,0);
			g[i].emplace_back(i,n+f[as[i]],1,-bs[i]);
			g[i].emplace_back(i,n+f[bs[i]],1,-as[i]);
			g[i].emplace_back(i,sink,1,0);
		}
		rep(i,m)
			g[n+i].emplace_back(n+i,sink,1,0);
		cout<<-MinCostFlow(g,tap,sink,n)<<endl;
	}
}