// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

#include <utility>

#include <queue>

#include <cstdlib>



struct point

{

    int x, y, org;

} p[100001];



int par[100001], rank[100001];



typedef std::pair<int, int> pi;

typedef std::pair<int, std::pair<int, int>> pii;

std::priority_queue<pii, std::vector<pii>, std::greater<pii>> edge;



int parent(int u)

{

    if (u == par[u])

        return u;

    else

        return par[u] = parent(par[u]);

}



void joint(int u, int v)

{

    if (rank[u] > rank[v])

    {

        par[v] = u;

        rank[u] += rank[v];

    }

    else

    {

        par[u] = v;

        rank[v] += rank[u];

    }

}



signed main()

{

    // freopen("a.inp", "r", stdin);

    std::ios_base::sync_with_stdio(false);

    std::cin.tie(0);

    std::cout.tie(0);



    int points, k;



    std::cin >> points;

    int x, y;

    for (int i = 0; i < points; ++i)

    {

        par[i] = i;

        p[i].org = i;

        std::cin >> p[i].x >> p[i].y;

    }

    std::sort(p, p + points, [](const point &p1, const point &p2) { return p1.x < p2.x; });

    for (int i = 0; i < points - 1; ++i)

        edge.push(pii(std::min(p[i + 1].x - p[i].x, abs(p[i + 1].y - p[i].y)), pi(p[i].org, p[i + 1].org)));

    std::sort(p, p + points, [](const point &p1, const point &p2) { return p1.y < p2.y; });

    for (int i = 0; i < points - 1; ++i)

        edge.push(pii(std::min(p[i + 1].y - p[i].y, abs(p[i + 1].x - p[i].x)), pi(p[i].org, p[i + 1].org)));

    unsigned s = 0;

    int tmp = 1;

    while (!edge.empty())

    {

        if (tmp == points)

            break;

        pii temp = edge.top();

        edge.pop();

        int cost = temp.first;

        int p_u = parent(temp.second.first);

        int p_v = parent(temp.second.second);

        if (p_u != p_v)

        {

            ++tmp;

            s += cost;

            joint(p_u, p_v);

        }

    }

    std::cout << s;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

#include <algorithm>



// 全局变量

const int N = 100010 , M = N + N;

int x[N],y[N],id[N],n;



typedef struct ufset *UFset;

struct ufset

{

  int *parent;

  int *root;

};

UFset UFinit(int size)

{

  int e;

  UFset U=(UFset)malloc(sizeof *U);

  U->parent=(int*)malloc((size+1)*sizeof(int));

  U->root=(int*)malloc((size+1)*sizeof(int));

  for(e=1; e<=size; e++)

  {

    U->parent[e]=1;

    U->root[e]=1;

  }

  return U;

}

int UFfind(int e,UFset U)

{

  int i,j = e;

  while(!U->root[j]) j = U->parent[j];

  while(j!=e){

    i = U->parent[e];

    U->parent[e]=j;

    e=i;

  }

  return j;

}

int UFunion(int i,int j,UFset U)

{

  if(U->parent[i]<U->parent[j])

  {

    U->parent[j]+=U->parent[i];

    U->root[i]=0;

    U->parent[i]=j;

    return j;

  }

  else

  {

    U->parent[i]+=U->parent[j];

    U->root[j]=0;

    U->parent[j]=i;

    return i;

  }

}



// 记录优化后的边集数

struct Edge

{

  int u, v, cost;

  bool operator <(const Edge&b) const

  {

    return cost < b.cost;

  }

} edge[M];

int tot;

// 按照X轴排序

bool cmpX(int a, int b)

{

  return x[a] < x[b];

}

// 按照Y轴排序

bool cmpY(int a, int b)

{

  return y[a] < y[b];

}

void add(int c, int a, int b)

{

  edge[tot].cost = c;

  edge[tot].u = a;

  edge[tot++].v = b;

}

void Work()

{

    scanf("%d", &n);

  int i;

  for (i = 1; i <= n; ++i)

  {

    scanf("%d%d", x + i, y + i);

    id[i] = i;

  }

  std::sort(id + 1 , id + n + 1, cmpX);

  for (i = 2; i <= n; ++i)

    add(x[id[i]] - x[id[i - 1]], id[i], id[i - 1]);

  std::sort(id + 1 , id + n + 1, cmpY);

  for (i = 2; i <= n; ++i)

    add(y[id[i]] - y[id[i - 1]], id[i], id[i - 1]);

  UFset UFS = UFinit(n);



  // 按照花费排序

  std::sort(edge, edge + tot);

  long long totcost = 0;

  for (i = 0; i < tot; ++i)

  {

    int u = edge[i].u, v = edge[i].v;

    u = UFfind(u,UFS), v = UFfind(v,UFS);

    if (u != v)

    {

      UFunion(u,v,UFS);

      totcost += edge[i].cost;

    }

  }

  printf("%lld\n", totcost);

}

int main()

{

  Work();

  return 0;

}


// Fastest known solution for this problem: