P17736 [HackerEarth] Walter White and Jesse Pinkman have now established their bases at different places.
提高
题目描述

Walter White and Jesse Pinkman have now established their bases at different places.
Now they want to form a network, that is, they want that all their bases should be reachable from every base.
One base is reachable from other base if there is a path of tunnels connecting bases.
Bases are suppose based on a 2-D plane having integer coordinates.
Cost of building tunnels between two bases are coordinates (x1,y1) and (x2,y2) is min{ |x1-x2|, |y1-y2| }.

What is the minimum cost such that a network is formed.

Input:
First line contains N, the number of bases.
Each of the next N lines contain two integers xi and yi.

Output:
Print the minimum cost of building the network.

Constraints:
1 ≤ N ≤ 100000
-10^9 ≤ xi,yi ≤ 10^9

SAMPLE INPUT
2
1 5
7 8

SAMPLE OUTPUT
3

输入格式

见题面

输出格式

见题面

样例输入
100 778935 -316712 -853288 -83696 -325757 240466 886034 -800785 84 -916928 -495434 -264224 -286674 -997151 -321446 -616144 547348 842173 -160713 921217 939728 -376396 -58750 913659 878590 593953 850807 844724 -322084 -922742 687446 -22369 643334 -813889 -369919 948553 -357626 -486819 -311911 -158208 166243 -537109 223644 -864524 -760229 -9478 -938136 90473 661092 201164 -739974 867752 952769 879104 -201418 -381298 492076 111639 -337062 -172055 668737 -967044 613319 -815951 -779608 -446566 781646 -947287 -925306 310683 -498400 141815 889527 -530622 965511 -493490 -799749 -468752 587118 -570048 497371 -473903 -672121 965866 2351 -913967 -18553 -188755 176391 -944853 -302660 428452 -361542 -64279 608327 -691920 827163 935009 226538 -445698 -938655 -105405 -888079 -158850 -582158 -833086 -831237 -183057 -744267 701213 -775936 -596564 68967 -397770 701325 -935768 394685 323256 -843997 985870 -317091 -72128 729518 -565266 91616 753973 -533583 599242 -259787 422164 -828107 192739 -996884 -758093 10695 -637686 171441 470077 -672946 673025 -851789 -774649 172213 -988092 -757888 -878981 232490 227904 929309 -776583 -741761 498058 886670 183426 411157 -754806 -744682 528051 434746 -257063 -212658 -839352 466289 -591994 -735728 -709296 39591 955983 383228 361885 -148764 -745156 -548732 -32136 -13756 -367434 503462 -261831 -886853 -842890 255467 276506 -938214 403861 -555890 -17764 241525 123497 141436 -456008 -916349 583158 207845 -722293 -61894 -367562 -162830 470959 489086 317277
样例输出
6417523