#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <random>
#include <iomanip>

using namespace std;
using P = pair<long, long>;

typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs, x)        \
  for (int i = 0; i < (x); i++) \
    scanf("%d", &xs[i]);
#define fillLong(xs, x)       \
  for (int i = 0; i < (x); i++) \
    scanf("%ld", &xs[i]);
#define fillDouble(xs, x)       \
  for (int i = 0; i < (x); i++) \
    scanf("%lf", &xs[i]);
#define fillString(xs, x)       \
  for (int i = 0; i < (x); i++) \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvinv(xs) sort(xs.begin(), xs.end(), std::greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define index_of(as, x) \
  distance(as.begin(), lower_bound(as.begin(), as.end(), x))
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define concat(xs, ys) (xs).insert((xs).end(), (ys).begin(), (ys).end())

const int mod = 1000000007;
const int MAX_V = 100005;

struct Point {
  int id;
  long x;
  long y;
};

struct UnionFind {
    vector<ll> par;
    vector<ll> siz;

    UnionFind(ll N) : par(N), siz(N) {
        for(int i = 0; i < N; i++) {
            par[i] = i;
            siz[i] = 1;
        }
    }

    ll root(ll x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    bool unite(ll x, ll y) {
        ll rx = root(x);
        ll ry = root(y);
        if (rx == ry) return false;
        if (siz[rx] < siz[ry]) swap(rx, ry);
        siz[rx] += siz[ry];
        par[ry] = rx;
        return true;
    }

    bool same(ll x, ll y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

struct QP {
  int id1;
  int id2;
  long dist;

  bool operator>(QP other) const {
    return dist > other.dist;
  }
};


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

  int n;
  cin >> n;

  vector<Point> points;
  rep(i, n) {
    long x, y;
    cin >> x >> y;
    points.push_back(Point({i, x, y}));
  }

  vector<Point> pointsSortByX = points;
  vector<Point> pointsSortByY = points;

  sort(pointsSortByX.begin(), pointsSortByX.end(), [](Point l, Point r) {
    return l.x < r.x;
  });

  sort(pointsSortByY.begin(), pointsSortByY.end(), [](Point l, Point r) {
    return l.y < r.y;
  });

  UnionFind uf(n);
  priority_queue<QP, vector<QP>, greater<QP>> que;
  long res = 0;

  rep(i, n-1) {
    if (pointsSortByX[i].x == pointsSortByX[i+1].x) {
      uf.unite(pointsSortByX[i].id, pointsSortByX[i+1].id);
    } else {
      que.push(QP({pointsSortByX[i].id, pointsSortByX[i+1].id, pointsSortByX[i+1].x - pointsSortByX[i].x}));
    }
    if (pointsSortByY[i].y == pointsSortByY[i+1].y) {
      uf.unite(pointsSortByY[i].id, pointsSortByY[i+1].id);
    } else {
      que.push(QP({pointsSortByY[i].id, pointsSortByY[i+1].id, pointsSortByY[i+1].y - pointsSortByY[i].y}));
    }
  }

  while(!que.empty()) {
    QP p = que.top();
    que.pop();

    if (uf.same(p.id1, p.id2)) continue;
    
    uf.unite(p.id1, p.id2);
    res += p.dist;
  }

  cout << res << endl;
}


Measured memory_bytes: 17392