using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
class Program
{
	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');//Splitで区切り文字を指定して複数個受け取る。
		long n = long.Parse(input[0]);
		long c = long.Parse(input[1]);
    long[,] vertexes = new long[n,2];//ここに受け取る
    long scoreMemo = 0;
    long answer = 0;
    for(long i = 0; i < n; i++)
    {
      long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);//１行ごとに受け取る
      vertexes[i,0] = nums[0];
      vertexes[i,1] = nums[1];
    }

    long eatPointR = 0;
    long eatPointL = 0;
    long minusR = 0;
    long minusL = 0;

    for(long eatNum = 0; eatNum <= n; eatNum++)
    {
    if(eatNum > 0)
      {
      eatPointR += vertexes[eatNum-1,1];
      minusR = vertexes[eatNum-1,0];
      }
      eatPointL = 0;
        for(long eatNumL = 0; eatNumL <= n-eatNum; eatNumL++)
        {
        if(eatNumL > 0) 
          {
            eatPointL += vertexes[n-eatNumL,1];
            minusL = c-vertexes[n-eatNumL,0];
          }else
          {
            minusL = 0;
          }
          //Console.WriteLine(eatPointL+" "+eatPointR
          //  +" "+minusL+" "+minusR);
          answer = Math.Max(answer, eatPointL+eatPointR
            -minusL-minusR-Math.Min(minusL, minusR));
        }
    }

		Console.WriteLine(answer);//WriteLineをWriteとすると、改行なしで出力。
	}
}