open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let Read3Int () = ReadNextInt (), ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module ARC076 =
    let private Mod = 1000000007L

    (* node index ranges from 0 to n-1 *)
    type DisjointSetArray (n) =
        let rootValue = -1
        let arr = Array.create n rootValue

        (* Gets the root node of the set x belongs to. *)
        member s.GetRoot x =
            if arr.[x] = -1 then x
            else
                arr.[x] <- s.GetRoot arr.[x]
                arr.[x]

        (* Gets the root nodes. *)
        member s.GetRoots () = [0..n-1] |> Seq.filter (fun x -> arr.[x] = rootValue)

        (* Merges the sets x and y each belongs to.
         * Input of f: root node of x and root node of y. *)
        member s.Merge x y f =
            let rx, ry = s.GetRoot x, s.GetRoot y
            if rx <> ry then arr.[rx] <- ry
            f rx ry

    let private Built () =
        let n = ReadNextInt ()
        let xs, ys =
            let rec loop i rx ry =
                if i = n then
                    rx |> Seq.toArray, ry |> Seq.toArray
                else
                    let x, y = Read2Int ()
                    loop (i + 1) (x :: rx) (y :: ry)
            loop 0 [] []
        let es = new System.Collections.Generic.List<_> ()
        let up s =
            let t =
                s
                |> Seq.mapi (fun i v -> v, i)
                |> Seq.sort
                |> Seq.toArray
            printfn "%A" t
            for c in [1..n-1] do
                let pv, j = t.[c - 1]
                let cv, i = t.[c]
                es.Add (abs (pv - cv), j, i)
        up xs
        up ys
        es.Sort ()
        printfn "%A" es
        let da = new DisjointSetArray (n)
        let mutable sum = 0L
        es |> Seq.iter (fun (d, i, j) ->
            da.Merge i j (fun fi fj ->
                if fi <> fj then sum <- sum + int64 d))
        sum |> printfn "%d"

    let Entry = Built

ARC076.Entry ()