using System;
using System.Collections.Generic;

namespace AtCoder.ARC096
{
    public struct Sushi
    {
        public long Distance { get; set; }
        public int Calorie { get; set; }
    }

    public class ARC096D
    {
        private int SushiNum { get; set; }

        private long LengthOfCounter { get; set; }

        private Sushi[] Sushis { get; set; }

        private IDictionary<int, long> LeftCalorieSum = new Dictionary<int, long>();

        private IDictionary<int, long> RightCalorieSum = new Dictionary<int, long>();

        public ARC096D(int sushiNum, long lengthOfCounter, Sushi[] sushis)
        {
            this.SushiNum = sushiNum;
            this.LengthOfCounter = lengthOfCounter;
            this.Sushis = sushis;
            this.InitCalorieSum();
        }

        private void InitCalorieSum()
        {
            long cal = 0;
            for (int index = 0; index < this.SushiNum; index ++)
            {
                cal = 0;
                for (int i = 0; i <= index; i++)
                {
                    cal += this.Sushis[i].Calorie;
                }
                this.LeftCalorieSum[index] = cal;
            }

            for (int index = 0; index < this.SushiNum; index++)
            {
                cal = 0;
                for (int i = index; i < this.SushiNum; i++)
                {
                    cal += this.Sushis[i].Calorie;
                }
                this.RightCalorieSum[index] = cal;
            }
        }

        public long GetMaxCal()
        {
            long maxCal = 0;
            long estimatedCal = 0;
            long lf = 0, rf = 0;
            int li = 0, ri = 0;
            for (int ln = 0; ln <= this.SushiNum; ln++)
            {
                for (int rn = 0; rn <= this.SushiNum - ln; rn++)
                {
                    li = ln - 1;
                    ri = rn == 0 ? -1 : this.SushiNum - rn;
                    estimatedCal = GetCalorieSum(this.Sushis, li, ri);

                    lf = estimatedCal;
                    rf = estimatedCal;

                    lf -= 0 <= li ? 2 * this.Sushis[li].Distance : 0;
                    lf -= 0 <= ri ? this.LengthOfCounter - this.Sushis[ri].Distance : 0;

                    rf -= 0 <= ri ? 2 * (this.LengthOfCounter - this.Sushis[ri].Distance) : 0;
                    rf -= 0 <= li ? this.Sushis[li].Distance : 0;

                    if (rf < lf)
                    {
                        maxCal = Math.Max(maxCal, lf);
                    }
                    else
                    {
                        maxCal = Math.Max(maxCal, rf);
                    }
                }
            }
            return maxCal;
        }

        public static void Main(string[] args)
        {
            string[] lineArgs = null;
            int sushiNum = 0;
            long lengthOfCounter = 0;
            Sushi[] sushis;

            lineArgs = Console.ReadLine().Split(' ');

            sushiNum = int.Parse(lineArgs[0]);
            lengthOfCounter = long.Parse(lineArgs[1]);
            sushis = new Sushi[sushiNum];

            for (int index = 0; index < sushiNum; index++)
            {
                lineArgs = Console.ReadLine().Split(' ');
                sushis[index] = new Sushi()
                {
                    Distance = long.Parse(lineArgs[0]),
                    Calorie = int.Parse(lineArgs[1]),
                };
            }

            Array.Sort(sushis, (Sushi x, Sushi y) => x.Distance.CompareTo(y.Distance));
            Console.WriteLine(new ARC096D(sushiNum, lengthOfCounter, sushis).GetMaxCal());
        }

        private long GetCalorieSum(Sushi[] sushis, int leftIndex, int rightIndex)
        {
            long cal = 0;
            if (0 <= leftIndex)
            {
                cal += this.LeftCalorieSum[leftIndex];
            }
            if (0 <= rightIndex)
            {
                cal += this.RightCalorieSum[rightIndex];
            }
            return cal;
        }
    }
}