using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("1 5");
WillReturn.Add("3 9");
WillReturn.Add("7 8");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("6");
WillReturn.Add("8 3");
WillReturn.Add("4 9");
WillReturn.Add("12 19");
WillReturn.Add("18 1");
WillReturn.Add("13 5");
WillReturn.Add("7 6");
//8
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
struct XYInfoDef
{
internal int Node;
internal int X;
internal int Y;
}
static List<XYInfoDef> mXYInfoList = new List<XYInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
mN = int.Parse(InputList[0]);
int Node = 0;
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
XYInfoDef WillAdd;
WillAdd.Node = Node++;
WillAdd.X = wkArr[0];
WillAdd.Y = wkArr[1];
mXYInfoList.Add(WillAdd);
}
Solve();
}
struct EdgeInfoDef
{
internal int FromNode;
internal int ToNode;
internal int Cost;
}
static List<EdgeInfoDef> mEdgeInfoList = new List<EdgeInfoDef>();
static void Solve()
{
// 辺を作成する
XYInfoDef[] mSortedXYArr1 = mXYInfoList.OrderBy(pX => pX.X).ThenBy(pX => pX.Y).ToArray();
XYInfoDef[] mSortedXYArr2 = mXYInfoList.OrderBy(pX => pX.Y).ThenBy(pX => pX.X).ToArray();
Action<XYInfoDef, XYInfoDef> AddAct = (p1, p2) =>
{
int FromNode = p1.Node;
int ToNode = p2.Node;
int Cost = DeriveCost(p1.X, p1.Y, p2.X, p2.Y);
EdgeInfoDef WillAdd;
WillAdd.FromNode = FromNode;
WillAdd.ToNode = ToNode;
WillAdd.Cost = Cost;
mEdgeInfoList.Add(WillAdd);
};
for (int I = 1; I <= mSortedXYArr1.GetUpperBound(0); I++) {
AddAct(mSortedXYArr1[I - 1], mSortedXYArr1[I]);
}
for (int I = 1; I <= mSortedXYArr2.GetUpperBound(0); I++) {
AddAct(mSortedXYArr2[I - 1], mSortedXYArr2[I]);
}
// クラスカル法で解く
var InsUnionFind = new UnionFind();
for (int I = 0; I <= mN - 1; I++) {
InsUnionFind.MakeSet(I);
}
// 枝をコストの昇順にソート
mEdgeInfoList.Sort((a, b) => a.Cost.CompareTo(b.Cost));
long SumCost = 0;
foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoList) {
int FromNode = EachEdgeInfo.FromNode;
int ToNode = EachEdgeInfo.ToNode;
int RootNode1 = InsUnionFind.FindSet(EachEdgeInfo.FromNode);
int RootNode2 = InsUnionFind.FindSet(EachEdgeInfo.ToNode);
if (RootNode1 != RootNode2) {
InsUnionFind.Unite(FromNode, ToNode);
SumCost += EachEdgeInfo.Cost;
}
}
Console.WriteLine(SumCost);
}
// 座標二つを引数として、道を作るコストを返す
static int DeriveCost(int pX1, int pY1, int pX2, int pY2)
{
int Kouho1 = Math.Abs(pX1 - pX2);
int Kouho2 = Math.Abs(pY1 - pY2);
return Math.Min(Kouho1, Kouho2);
}
}
#region UnionFind
// UnionFindクラス
internal class UnionFind
{
private class NodeInfoDef
{
internal int ParentNode;
internal int Rank;
}
private Dictionary<int, NodeInfoDef> mNodeInfoDict =
new Dictionary<int, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(int pNode)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(int pX, int pY)
{
int XNode = FindSet(pX);
int YNode = FindSet(pY);
int XRank = mNodeInfoDict[XNode].Rank;
int YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
}
}
// ノードを引数として、木の根を取得
internal int FindSet(int pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<int>();
int CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (int EachPathNode in PathNodeList) {
NodeInfoDef wkNodeInfo = mNodeInfoDict[EachPathNode];
wkNodeInfo.ParentNode = CurrNode;
mNodeInfoDict[EachPathNode] = wkNodeInfo;
}
return CurrNode;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}
#endregion