/*
                           _________________
                      ____/:::::::::::::::::\_____
                   __/::::::::::::::::::::::::::::\___
                 _/:::::::::::::::::::::::::::::::::::\__
               _/::::::::::::::::::::::::::::::::::::::::\_
              /::::::::::::::::::::::::::::::::::::::::::::\
             |::::::::::::::::::::::::::::::::::::::::::::::\
            /::::::::::::::::::::::::::::::::::::::::::::::::\
           |:::/.:::::::;:::::::::::::::::::::::::::::::::::::|
          /:::/.:::::::/..:::::::::::::::::::::::::::::::::::::\
         |:::|.::::::;/.::::::::::::::::::::::::::::::::::::::::|
         |::/.::::::/..:::::::;;'.::::::::::::::::::::::::::::::|
         |:|.::::/./.::::::;;/..:::::::::::::::::::::::::::::::::|
         `:|.:::|.|.:::::;/..;;;;;;-'.:;;;-':::::::::::::::::::::|
          \|.:::|.|.:::;/.;;/  -..::'''...:::::::::::::::::::::::|
           \;;::|.|.::/.;/--__      |::::::::::::::::::::::::::::|
              \;;\\::/|/ =-__ --_  /::::::::::::::::::::::::::::::
                  \\/    /|  -.   |.::::::::::::::::::::::::::::::
                 _.'    /|'   /=  ||::::::::::::::::::::::::::::::
             _.-'      //'        ||::::::::::::::::::::::::::::::
            (          -          `|::::::::::::::::::::::::::::::
             \                      \:::::::::::::::::::::::::::::
              |  (c)Kolansburg      \::::::::::::::::::::::::::::
             /                      __/:::::::::::::::::::::::::::
             \                   __/::;::;;:::::::::::::::::::::::
             (`                 /;;;;/::|  \::::::::::::::::::::::
             |                 |'_,::::/ \ |::::::::::::::::::::::
            .'                _/::::::/  / /:::::::::::::::. .::::
            |                /.::;;:-'_)/_/::::::::::::::::...::::
 ----.__    |               |.::|   \___/::::::::::::::::::...::::
 :::::::`----\_____          \:::\.-'::::::::::::::::::::::...::::
 ;;;;;:::::::::::::`------     \:::::::::::::::::::::::::::...::::
      `-------:::::::\         /:::::::::::::::::::::::::::...::::
 ___.--------'::::::::\       |::::::::::::::::::::::::::::...::::
 :::::;;;:--:::::::::::|      /::::::::::::::::::::::::::::...::::
 ----'  _,-:.:::::::::::\    |.::::::::::::::::::::::::::::...::::
     __/.::::::::::::::::|   |.::::::::::::::::::::::::::::...::::
  __/.:::;;::::::::;/.:::|   |.::::::::::::::::::::::::::::. .::::
 /.::::;/ /.:::::;/ |.::::|   \_.:::::::::::::::::::::::::::::::::
 :::::/  /.:::::/  /.:::::|     \__.::::::::::::::::::::::::::::::
 ::::|  |.:::::/  /.:::::.|        \,::::::::::::::::::::::::::::
 ::::|  |.::::|   |.:::::/|       __/::::::::::::::::::::::::::::
 \.:::\  \.:::|   |.::::||| __.--::::::::::::::::::::::::::::::::
  \.:::\_ \.:::\   \.:::'/.::::::::::::::::::::::::::::::::::::::
   \.::::\ \.:::\   \.::::::::::::::::::::::::::::::::::::::::::
*/
 
#define _CRT_SECURE_NO_WARNINGS
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <stack>
#include <random>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define X first
#define Y second

const int N = 100100;
int n, x, y, pr[N];
long long ans;
vector<pair<int, int> > gx, gy;
vector< pair<int, pair<int, int> > > g;

int get_pr(int v)
{
	if (pr[v] == v)
		return v;
	pr[v] = get_pr(pr[v]);
	return pr[v];
}

bool uni(int v, int u)
{
	v = get_pr(v);
	u = get_pr(u);
	if (u == v)
		return false;
	if (rand() % 2)
	{
		pr[v] = u;
	}
	else
	{
		pr[u] = v;
	}
	return true;
}

int main()
{
#if _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("parray.in", "r", stdin);
	//freopen("parray.out", "w", stdout);
#endif
	//ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		pr[i] = i;
		cin >> x >> y;
		gx.push_back({ x, i });
		gy.push_back({ y, i });
	}
	sort(gx.begin(), gx.end());
	sort(gy.begin(), gy.end());
	for (int i = 1; i < n; i++)
	{
		g.push_back({ gx[i].X - gx[i - 1].X, {gx[i].Y, gx[i - 1].Y} });
		g.push_back({ gy[i].X - gy[i - 1].X, {gy[i].Y, gy[i - 1].Y} });
	}
	sort(g.begin(), g.end());
	for (int i = 0; i < g.size(); i++)
	{
		if (uni(g[i].Y.X, g[i].Y.Y))
		{
			ans += g[i].X;
		}
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 7528