use std::io::*;
use std::cmp;

fn read<T: std::str::FromStr>() -> T {
    let stdin = stdin();
    let mut buf = String::new();
	stdin.lock().read_line(&mut buf);
	buf.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
	read::<String>().trim().split_whitespace()
        .map(|w| w.parse().ok().unwrap()).collect()
}

fn read_mat<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

fn main() {
    let v = read_vec::<i64>();
    let (n,c) = (v[0] as usize,v[1]);
    let sus = read_mat::<i64>(n);
    let totv: i64 = sus.iter().map(|s| s[1]).sum();
    let mut cw: Vec<(i64,i64)> = vec![(0,0)];
    let mut cwr: Vec<(i64,i64)> = vec![(0,0)];
    let mut ccw: Vec<(i64,i64)> = vec![(0,0)];
    let mut ccwr: Vec<(i64,i64)> = vec![(0,0)];
    let mut cw_t = 0;
    for i in 0..n {
        cw_t += sus[i][1];
        let mut tmp = cw_t - sus[i][0];
        if 0 < tmp {
            cw.push((sus[i][0],tmp));
        }
        tmp = totv + sus[i][1] - cw_t - (c - sus[i][0]);
        if 0 < tmp {
            ccw.push((c-sus[i][0],tmp));
        }
        if i < n / 2 {
            tmp = cw_t - 2 * sus[i][0];
            if 0 < tmp {
                cwr.push((c-sus[i][0],tmp));
            }
        } else {
            tmp = totv + sus[i][1] - cw_t - 2 * (c - sus[i][0]);
            if 0 < tmp {
                ccwr.push((sus[i][0],tmp));
            }
        }
    }
    cw.sort_by(|a,b| b.1.cmp(&a.1));
    ccw.sort_by(|a,b| b.1.cmp(&a.1));
    let cwr_max_opt = cwr.iter().map(|x| x.1 + search(&ccw,x.0)).max();
    let ccwr_max_opt = ccwr.iter().map(|x| x.1 + search(&cw,x.0)).max();
    let cwr_max = if let Some(t) = cwr_max_opt { t } else { 0 };
    let ccwr_max = if let Some(t) = ccwr_max_opt { t } else { 0 };
    let ans = cmp::max(cmp::max(cw[0].1,ccw[0].1),cmp::max(cwr_max,ccwr_max));
    println!("{}",ans);
}

fn search(v: &Vec<(i64,i64)>, n: i64) -> i64 {
    for e in v {
        if e.0 < n {
            return e.1;
        }
    }
    return 0;
}
