#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const long long INF = 1LL<<50;

typedef long long Weight;
struct Edge {
  int src, dst;
  Weight capacity;
  Weight cost;
  Edge(int src, int dst, Weight capacity, Weight cost) :
    src(src), dst(dst), capacity(capacity), cost(cost) { }
};

bool operator < (const Edge &e, const Edge &f) {
  return e.cost != f.cost ? e.cost > f.cost : // !!INVERSE!!
    e.capacity != f.capacity ? e.capacity < f.capacity :
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define RESIDUE(u,v) (capacity[u][v] - flow[u][v])
#define RCOST(u,v) (cost[u][v] + h[u] - h[v])
pair<Weight, Weight> minimumCostFlow(const Graph &g, int s, int t) {
  const int n = g.size();
  Matrix capacity(n, Array(n)), cost(n, Array(n)), flow(n, Array(n));
  REP(u,n) FOR(e,g[u]) {
    capacity[e->src][e->dst] += e->capacity;
    cost[e->src][e->dst] += e->cost;
  }
  pair<Weight, Weight> total; // (cost, flow)
  vector<Weight> h(n);

  for (Weight F = INF; F > 0; ) { // residual flow
    vector<Weight> d(n, INF); d[s] = 0;
    vector<int> p(n, -1);
    priority_queue<Edge> Q; // "e < f" <=> "e.cost > f.cost"
    for (Q.push(Edge(-2, s, 0, 0)); !Q.empty(); ) {
      Edge e = Q.top(); Q.pop();
      if (p[e.dst] != -1) continue;
      p[e.dst] = e.src;
      FOR(f, g[e.dst]) if (RESIDUE(f->src, f->dst) > 0) {
        if (d[f->dst] > d[f->src] + RCOST(f->src, f->dst)) {
          d[f->dst] = d[f->src] + RCOST(f->src, f->dst);
          Q.push( Edge(f->src, f->dst, 0, d[f->dst]) );
        }
      }
    }
    if (p[t] == -1) break;

    Weight f = F;
    for (int u = t; u != s; u = p[u])
      f = min(f, RESIDUE(p[u], u));
    for (int u = t; u != s; u = p[u]) {
      total.first += f * cost[p[u]][u];
      flow[p[u]][u] += f; flow[u][p[u]] -= f;
    }
    F -= f;
    total.second += f;
    REP(u, n) h[u] += d[u];
  }
  return total;
}

typedef long long ll;
int main() {
  int n;
  scanf("%d", &n);

  Graph g(n+n*2+3);
  map<int, int> mp;
  int cnt = 0;
  ll sum = 0;
  for (int i=0;i<n; ++i) {
    int a,b;
    scanf("%d %d", &a, &b);
    if (mp.count(a) == 0) {
      mp[a] = n+cnt++;
      g[mp[a]].push_back(Edge(mp[a], 3*n+2, 1, 0));
    }
    if (mp.count(b) == 0) {
      mp[b] = n+cnt++;
      g[mp[b]].push_back(Edge(mp[b], 3*n+2, 1, 0));
    }
    g[3*n+1].push_back(Edge(3*n+1,i,1,0));
    g[i].push_back(Edge(i,mp[a],1,a));
    if (a!=b)
      g[i].push_back(Edge(i,mp[b],1,b));
    g[i].push_back(Edge(i,3*n,1,a+b));
    sum += a + b;
  }
  g[3*n].push_back(Edge(3*n, 3*n+2, INF, 0));
  Graph G = g;
  for (int i=0; i<3*n+3; ++i) {
    for (int j=0; j<g[i].size(); ++j) {
      Edge e = g[i][j];
      G[e.dst].push_back(Edge(e.dst, e.src, 0, -e.cost));
      //printf("%d -> %d : %lld, %lld\n", e.src,e.dst,e.capacity,e.cost);
    }
  }
  
  pair<Weight,Weight> p = minimumCostFlow(G, 3*n+1, 3*n+2);
  cout << sum-p.first << endl;
//  cout << p.first << " " << p.second << endl;
}

Measured memory_bytes: 0