let inf = 101010101010101010
let _ = Scanf.scanf "%d" @@ fun n ->
  let a = Array.init (n+1) (fun i ->
    if i = 0 then (0, inf) else Scanf.scanf " %d %d" (fun x y -> -x, y)) in
  Array.sort compare a;
  Array.fold_left (fun (s, (x',y')) (x,y) ->
    s + min (x-x') (y-y'), (x,y)) (snd a.(0), a.(0)) a
  |> fst |> Printf.printf "%d\n"