Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

require 'prime'
require 'set'
require 'tsort'
include Math
def max(a,b);              a > b ? a : b                              end
def min(a,b);              a < b ?  a : b                             end
def swap(a,b);             a, b = b, a                                end
def gif;                   gets.to_i                                  end
def gff;                   gets.to_f                                  end
def gsf;                   gets.chomp                                 end
def gi;                    gets.split.map(&:to_i)                     end
def gf;                    gets.split.map(&:to_f)                     end
def gs;                    gets.chomp.split.map(&:to_s)               end
def gc;                    gets.chomp.split('')                       end
def pr(num);               num.prime_division                         end
def digit(num);            num.to_s.length                            end
def array(s,ini=nil);      Array.new(s){ini}                          end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}}          end
def rep(num);              num.times{|i|yield(i)}                     end
def repl(st,en,n=1);       st.step(en,n){|i|yield(i)}                 end
class UnionFindTree

  class ParArray < Hash
    def [] key
      self[key] = key if super(key).nil?
      super(key)
    end
  end

  class SizeArray < Hash
    def [] key
      self[key] = 1 if super(key).nil?
      super(key)
    end
  end

  def initialize()
    @par = ParArray.new
    @size = SizeArray.new
  end

  private

  def find(x)
    return x if x == @par[x]
    return @par[x] = find(@par[x])
  end

  public

  def unite(x, y)
    x = find(x)
    y = find(y)

    return nil if x == y
    x, y = y, x if @size[x] < @size[y]

    @par[y] = x
    @size[x] += @size[y]
  end

  def same?(x, y)
    return find(x) == find(y)
  end

  def size(x)
    return @size[find(x)]
  end

end

class PriorityQueue
  class Element
    include Comparable

    attr_accessor :data, :priority

    def initialize(data, priority)
      @data, @priority = data, priority
    end

    def <=>(other) #最大を出したい時は-を外す
        -@priority <=> -other.priority
    end
  end

  def initialize
    @elements = []
  end

  def push (element, priority)
    @elements << Element.new(element,priority)
  end

  def pop
    last_element_index = @elements.size - 1
    @elements.sort!
    @elements.delete_at(last_element_index)
  end
end


n = gif
x = PriorityQueue.new
y = PriorityQueue.new
n.times do |i|
  xx,yy = gi
  x.push i, xx
  y.push i, yy
end


cost = Hash.new
list = Hash.new
xx = x.pop
yy = y.pop
tx0 = xx.data
tx1 = xx.priority
ty0 = yy.data
ty1 = yy.priority
1.upto n-1 do |i|
  xx = x.pop
  yy = y.pop
  nx0, nx1, ny0, ny1 = xx.data, xx.priority, yy.data, yy.priority
  cost[2*i-2],list[2*i-2],cost[2*i-1],list[2*i-1] = nx1-tx1, [nx0,tx0], ny1-ty1, [ny0,ty0]
  tx0,tx1,ty0,ty1  = nx0,nx1,ny0,ny1
end
cost = cost.sort {|(k1,v1), (k2,v2)| v1 <=> v2 }
un = UnionFindTree.new
ans = 0

cost.each do |c|
  break if un.size(0) >= n
  unless un.same? list[c[0]][0],list[c[0]][1]
    un.unite list[c[0]][0],list[c[0]][1]
    ans += c[1]
  end
end
puts ans