// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

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

	{

		up[i] = -1;

	}

}



int Find(int x)

{

	if (up[x] < 0) return x;

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

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

	{

		scanf("%d%d", &p[i].x, &p[i].y);

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

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

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

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

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

	sort(q.begin(), q.end());

	makesets();

	long long res = 0;

	for (int i = 0; i < q.size(); i++)

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

// Faster C++ solution for the same problem (measured speedup: 1.3774957434068367x):
#include <bits/stdc++.h>



using namespace std;



typedef long long ll;



const int N = 1e5 + 5;

int parent[N];

int rnk[N];



int getParent(int x)

{

  if(parent[x] == x)

    return x;

  return parent[x] = getParent(parent[x]);

}



void connect(int x, int y)

{

  x = getParent(x), y = getParent(y);

  if(x == y)

    return;

  if(rnk[x] <= rnk[y])

  {

    parent[x] = y;

    rnk[y] += rnk[x];

  }

  else

  {

    parent[y] = x;

    rnk[x] += rnk[y];

  }

}



bool isconnected(int x, int y)

{

  return getParent(x) == getParent(y);

}



int main()

{

 ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

 int n;

 cin >> n;

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

  parent[i] = i, rnk[i] = 1;

 vector<pair<int, int>> points(n);

 vector<pair<int, pair<int, int>>> dist;

 vector<pair<pair<int, int>, int>> sortx, sorty;

 for(auto &x: points)

 {

   cin >> x.first >> x.second;

 }

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

 {

   sortx.push_back({points[i], i});

   sorty.push_back({{points[i].second, points[i].first}, i});

 }

 sort(sortx.begin(), sortx.end());

 sort(sorty.begin(), sorty.end());

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

 {

   int dif = sortx[i].first.first - sortx[i - 1].first.first;

   dist.push_back({dif, {sortx[i].second, sortx[i - 1].second}});

   dif = sorty[i].first.first - sorty[i - 1].first.first;

   dist.push_back({dif, {sorty[i].second, sorty[i - 1].second}});

 }

 sort(dist.begin(), dist.end());

 ll ans = 0;

 for(auto &x: dist)

 {

   int firstNode = x.second.first, secondNode = x.second.second;

   if(isconnected(firstNode, secondNode))

    continue;

   ans += x.first;

   connect(firstNode, secondNode);

 }

 cout << ans;

 return 0;

}