let () = Scanf.scanf "%d\n" @@ fun n ->
  let lrs = Array.init n @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun l r -> l, r in
  let f x = Array.fold_right (fun (l, r) -> ( + ) @@
    if x < l then l - x
    else if r < x then x - r
    else 0) lrs 0 in
  let l = Array.fold_right (fun (l, _) -> min l) lrs max_int in
  let r = Array.fold_right (fun (_, r) -> max r) lrs 0 in
  Printf.printf "%d\n" @@
  Array.fold_left min max_int @@
  Array.init (r - l + 1) @@ fun i -> f @@ i + l
