using System;

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

    public struct LeftAndRightIndex
    {
        public int Left { get; set; }
        public int Right { get; set; }
    }

    public class ARC096D
    {
        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]),
                    Ate = false,
                };
            }

            Array.Sort(sushis, (Sushi x, Sushi y) => x.Distance.CompareTo(y.Distance));
            long calorie = GetCalorieMax(0, sushiNum, lengthOfCounter, sushis, 0, 0);
            Console.WriteLine(calorie);
        }

        private static long GetCalorieMax(int indent, int sushiNum, long lengthOfCounter, Sushi[] sushis, int startingIndex, long distance)
        {
            LeftAndRightIndex indexes = GetLeftAndRightIndex(sushiNum, sushis, startingIndex);
            if (indexes.Left == startingIndex && indexes.Right == startingIndex)
            {
                return 0;
            }

            // left
            long leftCalorie = sushis[indexes.Left].Calorie - GetClockwiseDistance(lengthOfCounter, sushis[indexes.Left].Distance, distance);
            sushis[indexes.Left].Ate = true;
            long leftCalorieEstimate = leftCalorie + GetCalorieMax(indent + 1, sushiNum, lengthOfCounter, sushis, indexes.Left, sushis[indexes.Left].Distance);
            if (leftCalorie < leftCalorieEstimate)
            {
                leftCalorie = leftCalorieEstimate;
            }
            sushis[indexes.Left].Ate = false;

            // right
            long rightCalorie = sushis[indexes.Right].Calorie - GetCounterclockwiseDistance(lengthOfCounter, distance, sushis[indexes.Right].Distance);
            sushis[indexes.Right].Ate = true;
            long rightCalorieEstimate = rightCalorie + GetCalorieMax(indent + 1, sushiNum, lengthOfCounter, sushis, indexes.Right, sushis[indexes.Right].Distance);
            if (rightCalorie < rightCalorieEstimate)
            {
                rightCalorie = rightCalorieEstimate;
            }
            sushis[indexes.Right].Ate = false;

            return leftCalorie < rightCalorie ? rightCalorie : leftCalorie;
        }

        private static long GetClockwiseDistance(long lengthOfCounter, long disLeft, long disRight)
        {
            if (disLeft - disRight < 0)
            {
                return lengthOfCounter + disLeft - disRight;
            }
            else
            {
                return disLeft - disRight;
            }
        }

        private static long GetCounterclockwiseDistance(long lengthOfCounter, long disLeft, long disRight)
        {
            if (disLeft < disRight)
            {
                return lengthOfCounter + (disLeft - disRight);
            }
            else
            {
                return disLeft - disRight;
            }
        }

        private static LeftAndRightIndex GetLeftAndRightIndex(int sushiNum, Sushi[] sushis, int startingIndex)
        {
            LeftAndRightIndex indexes = new LeftAndRightIndex()
            {
                Left = startingIndex,
                Right = startingIndex,
            };

            int candidateIndex = 0;

            // left
            for (int index = 0; index < sushiNum; index++)
            {
                candidateIndex = GetCirculatedIndex(startingIndex + index, sushiNum);
                if (sushis[candidateIndex].Ate == false)
                {
                    indexes.Left = candidateIndex;
                    break;
                }
            }

            // right
            for (int index = 1; index <= sushiNum; index++)
            {
                candidateIndex = GetCirculatedIndex(startingIndex - index, sushiNum);
                if (sushis[candidateIndex].Ate == false)
                {
                    indexes.Right = candidateIndex;
                    break;
                }
            }

            return indexes;
        }

        private static int GetCirculatedIndex(int index, int length)
        {
            if (length <= index)
            {
                return index - length;
            }
            else if (index < 0)
            {
                return length + index;
            }
            return index;
        }
    }
}