package yukicoder; import java.util.Scanner; import static java.lang.Math.max; public class No561 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), d = sc.nextInt(); int t, k, more, result = 0, resultK = -d; boolean tokyo = true; for(int i = 0; i < n; i++) { t = sc.nextInt(); k = sc.nextInt(); if( tokyo ) { more = max(t, k - d); if(t != k - d && more == k - d) tokyo = false; result += more; } else { more = max(t - d, k); if(t - d != k && more == t - d) tokyo = true; result += more; } resultK += k; } System.out.println(result < resultK ? resultK : result); } }