#!python
# -*- coding: utf-8 -*-

import sys
sys.setswitchinterval(1000000)


N, C = list(map(int, input().split(' ')))
X = []
V = []
for n in range(N):
    X_, V_ = list(map(int, input().split(' ')))
    X.append(X_)
    V.append(V_)


def sushi(sum_v, ichi, xs, vs):
    e = sum_v
    if len(xs) == 0:
        return e
    if len(xs) == 1:
        m = (C + xs[0] - ichi) % C
        move_v = min(m, C - m)
        return max(e, sum_v - move_v + vs[0])

    if ichi < xs[0]:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    elif xs[-1] < ichi:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    else:
        for i in range(len(xs) - 1):
            if xs[i] < ichi < xs[i + 1]:
                R_next_idx, L_next_idx = (i + 1, i)
                break

    # 右周り
    R_x = xs[R_next_idx]
    R_v = sum_v + vs[R_next_idx]
    R_v -= (C + R_x - ichi) % C
    R_xs = xs[:R_next_idx] + xs[R_next_idx + 1:]
    R_vs = vs[:R_next_idx] + vs[R_next_idx + 1:]

    # 左周り
    L_x = xs[L_next_idx]
    L_v = sum_v + vs[L_next_idx]
    L_v -= (C + ichi - L_x) % C
    L_xs = xs[:L_next_idx] + xs[L_next_idx + 1:]
    L_vs = vs[:L_next_idx] + vs[L_next_idx + 1:]

    r = sushi(R_v, R_x, R_xs, R_vs)
    l = sushi(L_v, L_x, L_xs, L_vs)

    return max(e, r, l)

if N <= 100:
    print(sushi(0, 0, X, V))
