//============================================================================
// Name : mmmmmmmm.cpp
// Author : sameh mohamed
// Copyright : don't try i'm warnning you......
// Description : Hello World
//============================================================================
//Egypt came then History
//Pharoas were coders but using different compilers{}{}{}{}{}}}}{{{{@#$%^^&&$$$###()()(***)))([][][][][]]][
#include<bits/stdc++.h>
#include <iostream>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
long long ss(long long n)
{
long long c=0;
while(n)
{
long long m=log2(n);
n=n-exp2(m);
c++;
}
return c;
}
long long gcd(long long a,long long b)
{
if(a==0)
return b;
else if(b==0)
return a;
else
while(a!=b)
{
if(a>b)
a-=b;
else
b-=a;
}
return a;
}
long long minc=1e9;
int main()
{
int n,d;
cin>>n>>d;
vector<long long>a(n),b(n);
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
long long m=min(a[i],b[i]);
minc=min(minc,m);
}
long long w=minc+minc+1+2*d;
long long s=0;
for(int i=0;i<n;i++)
{
long long h=min(a[i],b[i]);
long long m=min(a[i]+b[i],h+d+minc);
m=min(w,m);
s+=m;
}
cout<<s;
return 0;
}
Copy