open Printf
open Scanf

let id x = x
             
let rec read f n =
  if n <= 0 then []
  else
    let x = f () in
    x :: read f (n - 1)

let rec remove x = function
    [] -> []
  | h :: t when h = x -> t
  | h :: t -> h :: remove x t

let cost p1 p2 =
  let d1 = abs (fst p1 - fst p2) and
      d2 = abs (snd p1 - snd p2) in
  min d1 d2
                     
let () =
  let n = scanf "%d\n" id in
  let ps =
    let f () = scanf "%d %d\n" (fun x y -> (x, y)) in
    read f n in
  let rec iter xs ys c =
    match ys with
      [] -> c
    | _ -> let (mp, mc) = List.fold_left (
                              fun (tp, tc) x -> let (up, uc) = 
                                                  List.fold_left (fun (sp, sc) y ->
                                                                  let vc = cost x y in
                                                                  if vc < sc then (y, vc) else (sp, sc)
                                                                 ) ((-1, -1), 1000000000) ys in
                                                if uc < tc then (up, uc) else (tp, tc)
                            ) ((-1, -1), 1000000000) xs in
           iter (mp :: xs) (remove mp ys) (c+mc)
  in
  iter [(List.hd ps)] (List.tl ps) 0 |> printf "%d\n"
