#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define lint long long int /* vectoral(26); al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; vectorAL(26); AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; */ int main() { lint N, D; cin >> N >> D; vectorT(N); vectorK(N); for (int i = 0; i < N; i++) { cin >> T[i] >> K[i]; } vectordp(N); dp[0] = max(T[0], K[0] - D); for (int i = 1; i < N; i++) { if (dp[i - 1] == T[i - 1] || dp[i - 1] == T[i - 1] - D) { dp[i] = max(T[i], K[i] - D); } else if (dp[i - 1] == K[i - 1] || dp[i - 1] == K[i - 1] - D) { dp[i] = max(K[i], T[i] - D); } } /*for (int i = 0; i < N; i++) { cout << dp[i] << ' '; } cout << endl;*/ lint sum = 0; for (int i = 0; i < N; i++) { sum += dp[i]; } cout << sum << endl; }