Scanf.scanf "%d" (fun n ->
    let lr = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun l r -> l, r)) in
    let lp = Array.fold_left (fun acc (l, r) -> max acc l) 0       lr in
    let rq = Array.fold_left (fun acc (l, r) -> min acc r) max_int lr in
    let ab = Array.init n (fun i ->
        let l, r = lr.(i) in
        max (r - lp) 0, - max (rq - l) 0, i)
    in
    Array.sort compare ab;
    let s = Array.make (n - 1) 0 in
    let rec loop i l r =
        if i = n - 1 then () else
            let (_, _, j) = ab.(i) in
            let ll, rr = lr.(j) in
            let l = max ll l in
            let r = min rr r in
            let () = s.(i) <- max 0 (r - l + 1) in
            loop (i + 1) l r
    in
    loop 0 0 max_int;

    let rec loop i l r =
        if i = 0 then () else
            let (_, _, j) = ab.(i) in
            let ll, rr = lr.(j) in
            let l = max ll l in
            let r = min rr r in
            let () = s.(i - 1) <- s.(i - 1) + max 0 (r - l + 1) in
            loop (i - 1) l r
    in
    loop (n - 1) 0 max_int;

    Array.fold_left max 0 s |> Printf.printf "%d\n"
)