# input
N, C = map(int, input().split(" "))
x, v = [], []
for i in range(1, N+1):
    xv = list(map(int, input().split(" ")))
    x.append(xv[0])
    v.append(xv[1])

# x_i メートル
# v_i キロカロリー
max_cal = 0
S = [[i for i in range(N)]]  # stack
place = 0  # 位置
cal = 0
S[0].append(cal)
S[0].append(place)
while len(S) > 0:
    s = S.pop()  # n = len
    # index -1 : place
    # index -2 : calorie
    if len(s) == 3:
        # len(s) = 1 になったとき
        idx = s[0]  # first
        next_place = x[idx]
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[s[0]]
        max_cal = max(max_cal, next_cal)
    else:
        #while len(s) > 1:
        # x[s[0]] は s[0] 番目の寿司の相手ある絶対位置（初期値からの距離）
        place = s[-1]
        idx = s[0]  # first
        next_place = x[idx]
        dist = abs(next_place - place)
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[1:-2] + [next_cal, next_place])
        place = s[-1]
        idx = s[-3]  # last
        next_place = x[idx]
        #place_step = abs(next_place - place) % C
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[:-3] + [next_cal, next_place])
print(max_cal)