//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//                                                     _        _ _ _____  ___
//                               _ __ ___   __ _ _ __ | |_ __ _/ / |___ / / _ \
//                              | '_ ` _ \ / _` | '_ \| __/ _` | | | |_ \| | | |
//                              | | | | | | (_| | | | | || (_| | | |___) | |_| |
//                              |_| |_| |_|\__,_|_| |_|\__\__,_|_|_|____/ \___/

//https://github.com/manta1130/Competitive_Programming_Template_Rust

#[macro_use]
mod input {

    use std;
    use std::io;

    const SPLIT_DELIMITER: char = ' ';

    #[macro_export]
    #[allow(unused_macros)]
    macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

    #[allow(dead_code)]
    pub fn input_line_str() -> String {
        let mut s = String::new();
        io::stdin().read_line(&mut s).unwrap();
        s.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn p<T>(t: T)
    where
        T: std::fmt::Display,
    {
        println!("{}", t);
    }

    #[allow(dead_code)]
    pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<Vec<T>> = Vec::new();

        for _ in 0..line {
            let vec_line = input_vector();
            v.push(vec_line);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector<T>() -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<T> = Vec::new();

        let s = input_line_str();
        let split_result = s.split(SPLIT_DELIMITER);
        for z in split_result {
            let buf = match z.parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector_row<T>(n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::with_capacity(n);
        for _ in 0..n {
            let buf = match input_line_str().parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    pub trait ToCharVec {
        fn to_charvec(&self) -> Vec<char>;
    }

    impl ToCharVec for String {
        fn to_charvec(&self) -> Vec<char> {
            self.to_string().chars().collect::<Vec<_>>()
        }
    }
}

mod segtree {

///汎用のセグメント木
pub struct SegTreeBasic<T, F> {
    //n: usize,
    v: Vec<T>,
    init: T,
    parent_generator: F,
}

impl<T, F> SegTreeBasic<T, F>
where
    T: PartialOrd + Clone + Copy,
    F: Fn(T, T) -> T,
{
    ///セグメント木を構築する。
    ///
    /// # Examples
    /// ```ignore
    /// let mut segtree = SegTreeBasic::new(100, usize::max_value(), |a, b| std::cmp::min(a, b));
    /// //要素数100のRMQを構築する。
    /// ```
    pub fn new(n: usize, init: T, parent_generator: F) -> SegTreeBasic<T, F> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let v = vec![init; size * 2 - 1];
        SegTreeBasic {
            //n: size,
            v: v,
            init: init,
            parent_generator: parent_generator,
        }
    }

    ///[l,r)の要素をparent_generatorに基づいて計算する。
    pub fn get(&mut self, l: usize, r: usize) -> T {
        let vlen = self.v.len();
        self._get(l, r, 0, 0, (vlen + 1) / 2)
    }

    fn _get(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize) -> T {
        if a >= r || b <= l {
            self.init
        } else if l <= a && b <= r {
            self.v[now]
        } else {
            let bufa = self._get(l, r, now * 2 + 1, a, (a + b) / 2);
            let bufb = self._get(l, r, now * 2 + 2, (a + b) / 2, b);
            let cond = &self.parent_generator;
            cond(bufa, bufb)
        }
    }

    ///任意の要素を書き換える。
    pub fn update(&mut self, index: usize, value: T) {
        let offset = (self.v.len() + 1) / 2 - 1;
        self.v[offset + index] = value;
        let mut parent = index + offset;
        loop {
            parent = (parent - 1) / 2;
            let cond = &self.parent_generator;
            self.v[parent] = cond(self.v[parent * 2 + 1], self.v[parent * 2 + 2]);
            if parent == 0 {
                break;
            }
        }
    }
}

}



use input::*;
use segtree::*;

fn main() {
    let n: usize;
    input!(n);
    let mut v = vec![];
    for _ in 0..n {
        let (l, r): (usize, usize);
        input!(l, r);
        v.push((l, r));
    }
    v.sort();
    let mut tomin = SegTreeBasic::new(n, usize::max_value(), |a, b| std::cmp::min(a, b));
    let mut frommax = SegTreeBasic::new(n, usize::min_value(), |a, b| std::cmp::max(a, b));

    for i in 0..n {
        tomin.update(i, v[i].1);
        frommax.update(i, v[i].0);
    }

    let mut ans = 0_usize;
    for i in 1..n {
        let lto = tomin.get(0, i);
        let lfrom = frommax.get(0, i);
        let rto = tomin.get(i, n);
        let rfrom = frommax.get(i, n);
        let mut lsum = 0;
        let mut rsum = 0;
        if lto >= lfrom {
            lsum = lto - lfrom + 1;
        }
        if rto >= rfrom {
            rsum = rto - rfrom + 1;
        }
        ans = std::cmp::max(ans, lsum + rsum);
    }
    for i in 1..n-1 {
        let mto = tomin.get(i, i+1);
        let mfrom = frommax.get(i, i+1);
        let mut rto=usize::max_value();
        let mut rfrom=usize::min_value();
            rto=std::cmp::min(rto,tomin.get(i+1,n));
            rfrom=std::cmp::max(rfrom,frommax.get(i+1,n));
            rto=std::cmp::min(rto,tomin.get(0,i));
            rfrom=std::cmp::max(rfrom,frommax.get(0,i));

        let mut msum = 0;
        let mut rsum = 0;
        if mto >= mfrom {
            msum = mto - mfrom + 1;
        }
        if rto >= rfrom {
            rsum = rto - rfrom + 1;
        }
        ans = std::cmp::max(ans, msum + rsum);
    }

    p(ans);
}


Measured memory_bytes: 10492