#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define re(i,a,b) for(int i=(a);i<(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a) memset(a,0,sizeof(a));
#define il inline
#define sz(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back 
#define w1 first
#define w2 second
#define adm(a,b,c) {a=a+b;if(a>=c)a-=c;else if(a<0)a+=c;}
typedef double db;
typedef long long ll;typedef long double ld;typedef unsigned long long ull;
typedef pair<ll,ll> pa;
const int N=5e5+5,M=1e7+5,INF=1e9,mod=998244353;
const ll linf=1e18;const double eps=1e-8,pi=acos(-1);
il int gmin(int &a,int b){if(a>b)a=b;}il ll gmin(ll &a,ll b){if(a>b)a=b;}il int gmax(int &a,int b){if(a<b)a=b;}il ll gmax(ll &a,ll b){if(a<b)a=b;}
il void read(ll&x){ll f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il ll read(ll&x,ll&y){read(x);read(y);}
il void read(int&x){int f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}x=t*f;}il int read(int&x,int&y){read(x);read(y);}
il void read(int&a,int&b,int&c){read(a);read(b);read(c);}il void read(ll&a,ll&b,ll&c){read(a);read(b);read(c);}
il int read(){int x;read(x);return x;}
il ll qpow(ll a,ll b,ll p){ll ret=1;for(;b;b>>=1,a=a*a%p)if(b&1)ret=ret*a%p;return ret;}il ll qpow(ll a,ll b){ll ret=1;for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;return ret;}
il ll qmul(ll a,ll b,ll p){ll ret=0;for(;b;b>>=1,a=(a<<1)%p)if(b&1)adm(ret,a,p);return ret;}il ll qmul(ll a,ll b){ll ret=0;for(;b;b>>=1,a=(a<<1)%mod)if(b&1)adm(ret,a,mod);return ret;}
il void judge(){
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
}
vector<int>o;
int n,k;
ll f[N];
#define ls (k<<1)
#define rs (ls|1)
#define mid (l+r>>1)
#define lson ls,l,mid
#define rson rs,mid+1,r
void build(int k,int l,int r){
	if(l==r){f[k]=-o[l];return;}
	build(lson);build(rson);
	f[k]=min(f[ls],f[rs]);
}
ll ask(int k,int l,int r,int x,int y){
	if(x>y)return linf;
	if(f[k]==linf)return linf;
	if(l==x&&r==y){ll res=f[k];f[k]=linf;return res;}
	ll res;
	if(y<=mid)res=ask(lson,x,y);else
	if(x> mid)res=ask(rson,x,y);else
		res=min(ask(lson,x,mid),ask(rson,mid+1,y));
	f[k]=min(f[ls],f[rs]);
	return res;
}void modi(int k,int l,int r,int x,ll val){
	if(l==r){gmin(f[k],val-o[l]);return;}
	if(f[k]==linf)f[ls]=f[rs]=linf;
	if(x<=mid)modi(lson,x,val);else modi(rson,x,val);
	f[k]=min(f[ls],f[rs]);
}ll calc(int k,int l,int r){
	if(f[k]==linf)return linf;
	if(l==r)return o[l]+f[k];
	return min(calc(lson),calc(rson));
}
int l[N],r[N];
ll mt(ll x,ll k){x%=k;x=x<0?x+k:x;return x;}
#define rt 1,0,m
int main(){//judge();
	ll res=0,s=0;
	read(n,k);rep(i,1,n){
		int a,b;read(a,b);res+=a+a;
		if(b==1){
			if(a+a>k)return puts("-1"),0;
			l[i]=mt(s-2*a,k);r[i]=s;
		}else l[i]=k-1,r[i]=0;
		s=mt(s-2*a,k);
	}
	rep(i,1,n)o.pb(l[i]),o.pb(r[i]);
	sort(all(o));
	o.erase(unique(all(o)),o.end());
	//printf("(%d)",o.size());
	rep(i,1,n){
		l[i]=lower_bound(all(o),l[i])-o.begin();
		r[i]=lower_bound(all(o),r[i])-o.begin();
	}
	int m=o.size()-1;
	build(rt);
	rep(i,1,n){
		if(l[i]<r[i]){
			if(r[i]-l[i]>1)modi(rt,r[i],ask(rt,l[i]+1,r[i]-1)+o[r[i]]);
		}else{
			modi(rt,r[i],min(ask(rt,l[i]+1,m)+k,ask(rt,0,r[i]-1))+o[r[i]]);
		}
	}
	res+=calc(rt);
	cout<<res<<endl;
}

Measured memory_bytes: 5748