#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>

long long mod = 1e9+7;
class Mint {
public:
  long long x;
  Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
  Mint(long long a){
    x = a%mod;
    while(x<0){
      x += mod;
    }
  };
  Mint& operator += (const Mint &a){
    x+=a.x;
    x%=mod;
    return *this;
  }
  Mint& operator -= (const Mint &a){
    x += (mod - a.x);
    x%=mod;
    return *this;
  }
  Mint& operator *= (const Mint &a){
    x*=a.x;
    x%=mod;
    return *this;
  }

  // a^n mod を計算する
  // Useaeg: Mint z = y.pow(y, n);
  Mint pow(Mint a, long long n) const {
    Mint y(1);
    Mint b(a);
    while (n > 0) {
      if (n & 1) y *= b;
      b *= b;
      n >>= 1;
    }
    return y;
  }

  // a^{-1} mod を計算する
  Mint modinv(const Mint &a) const {
    return pow(a, mod-2);
  }

  Mint& operator /= (const Mint &a) {
    x *= modinv(a).x;
    x %= mod;
    return *this;
  }

  Mint operator + (Mint &a) const {
    Mint y(*this);
    y += a;
    return y;
  }

  Mint operator - (Mint &a) const {
    Mint y(*this);
    y -= a;
    return y;
  }

  Mint operator * (Mint &a) const {
    Mint y(*this);
    y *= a;
    return y;
  }

  Mint operator / (Mint &a) const {
    Mint y(*this);
    y/=a;
    return y.x;
  }

  // nCk @mod を計算する
  Mint nCk(Mint &n, const long long k) const {
    Mint y = Mint(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
      y /= i;
    }
    return y;
  }

  // nPk @mod を計算する
  Mint nPk(Mint &n, long long k) const {
    Mint y(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
    }
    return y;
  }
};

struct point {
  int i;
  int x;
  int y;
};

struct edge {
  int i1;
  int i2;
  int dist;
};

bool CompareX (const point &p1, const point &p2) {
  return p1.x < p2.x;
}

bool CompareY (const point &p1, const point &p2) {
  return p1.y < p2.y;
}

class CompareFirst {
  public:
    bool operator()(edge p1, edge p2) {
      return p1.dist > p2.dist;
    }
};


int parent[100004];
int length[100004];
int num[100004];

void init(){
  for (int i=0; i<100004; i++) {
    parent[i] = i;
    length[i] = 1;
    num[i] = 1;
    //printf("num[%d]=%d\n", i, num[i]);
  }
  return;
}

int getroot(int n) {
  if (n==parent[n]) return n;
  int orig = n;
  while(n!=parent[n]) {
    n = parent[n];
  }
  parent[orig] = n;
  return n;
}

bool samegroup(int n, int m) {
  int nroot = getroot(n);
  int mroot = getroot(m);
  return nroot==mroot;
}

void unite(int n, int m) {
  if (samegroup(n, m)) return;
  int nroot = getroot(n);
  int mroot = getroot(m);
  if (length[mroot]< length[nroot]) {
    parent[mroot] = nroot;
    num[nroot] += num[mroot];
  } else if (length[nroot] < length[mroot]) {
    parent[nroot] = mroot;
    num[mroot] += num[nroot];
  } else {
    parent[nroot] = mroot;
    num[mroot] += num[nroot];
    length[mroot]++;
  }
  return;
}

int main(){
  int N;
  std::cin >> N;
  int x, y;
  init();
  std::vector<point> data;
  for (int i=0; i<N; i++) {
    std::cin >> x >> y;
    data.push_back((point){i, x, y});
  }
  //printf("read end\n");
  //printf("num0=%d\n", getroot(0));
  // X
  std::sort(data.begin(), data.end(), CompareX);
  std::priority_queue<edge, std::vector<edge >, CompareFirst > queue;
  for (int i=0; i<N-1; i++) {
    //printf("x[%d] = %d\n", i, data[i].x);
    int diff = data[i+1].x - data[i].x;
    if (diff == 0) {
      unite(data[i+1].i, data[i].i);
    } else {
      queue.push((edge){data[i+1].i, data[i].i, diff});
      //printf("pushing(%d, %d) dist=%d\n", data[i+1].i, data[i].i, diff);
    }
  }

  // Y
  std::sort(data.begin(), data.end(), CompareY);
  for (int i=0; i<N-1; i++) {
    int diff = data[i+1].y - data[i].y;
    if (diff == 0) {
      unite(data[i+1].i, data[i].i);
    } else {
      queue.push((edge){data[i+1].i, data[i].i, diff});
      //printf("pushing(%d, %d) dist=%d\n", data[i+1].i, data[i].i, diff);
    }
  }

  // unite
  int num0 = num[getroot(0)];
  long long sum = 0;
  while(num0 < N) {
    auto entry = queue.top();
    queue.pop();
    int i = entry.i1;
    int j = entry.i2;
    int d = entry.dist;
    if (samegroup(i, j)) continue;
    sum += d;
    //printf("d=%d, unite (%d, %d)\n", d, i, j);
    unite(i, j);
    num0 = num[getroot(0)];
    //printf("num0=%d\n", num0);
  }
  std::cout << sum << std::endl;
  return 0;
}

Measured memory_bytes: 8240