import random def does_change(cur_place, cur_day): a = 0 b = 0 for i in range(cur_day, N): a += money[i][cur_place] b += money[i][not cur_place] if (a >= b): return False if (a < b - 2 * D): return True if (a < b - D): return True else: return False N, D = map(int, input().split()) money = [list(map(int, input().split())) for i in range(N)] cur_place = 0 cur_v = 0 for i in range(N): if (does_change(cur_place, i)): cur_place = not cur_place cur_v -= D cur_v += money[i][cur_place] print(cur_v)