#include<bits/stdc++.h>
typedef long long i64;
int n,k;
i64 sa=0;
void mins(i64&a,i64 b){if(a>b)a=b;}
struct node{
	node*lc,*rc;
	int D,sz,rnd;
	i64 vl,vr;
	void sp(node*&,node*&,int);
	i64 gl(){return lc?lc->gl():vl;}
	i64 gmn(){
		i64 mn=vr;
		if(lc)mins(mn,lc->gmn());
		if(rc)mins(mn,rc->gmn());
		return mn;
	}
}ns[1000007],*np=ns,*rt,*p1,*p2,*p3,*p4;
node*mg(node*a,node*b){
	if(!a)return b;
	if(!b)return a;
	if(a->rnd>b->rnd){
		a->sz+=b->sz;
		a->rc=mg(a->rc,b);
		return a;
	}
	b->sz+=a->sz;
	b->lc=mg(a,b->lc);
	return b;
}
void node::sp(node*&l,node*&r,int k){
	if(!this){l=r=0;return;}
	int ls=lc?lc->sz:0;
	if(k<=ls)return r=this,sz-=k,lc->sp(l,lc,k);
	if(k>=ls+D)return l=this,sz=k,rc->sp(rc,r,k-ls-D);
	k-=ls;
	node*a=np++,*b=np++;
	*a=(node){0,0,k,k,rand(),vl,vl};
	*b=(node){0,0,D-k,D-k,rand(),vr,vr};
	if(vl>vr){
		a->vr=vl-k+1;
		b->vl=vl-k;
	}
	l=mg(lc,a);
	r=mg(b,rc);
}
char buf[2000007],*ptr=buf;
int _(){
	int x=0;
	while(*ptr<48)++ptr;
	while(*ptr>47)x=x*10+*ptr++-48;
	return x;
}
int main(){
	fread(buf,1,sizeof(buf),stdin);
	n=_(),k=_();
	srand(time(0));
	*(rt=np++)=(node){0,0,k,k,rand(),0,0};
	for(int i=1,a,b;i<=n;++i){
		a=_(),b=_();
		sa+=a;
		if(b==1){
			if(a*2>k)return puts("-1"),0;
			rt->sp(p1,p2,1);
			p2->sp(p2,p3,a*2-1);
			p3=mg(p3,p1);
			i64 y=p3->gl();
			*(p4=np++)=(node){0,0,a*2-1,a*2-1,rand(),y+a*2-1,y+1};
			rt=mg(p3,p4);
		}else{
			rt->sp(p1,p2,a*2%k);
			rt=mg(p2,p1);
		}
	}
	printf("%lld\n",sa*2+rt->gmn());
	return 0;
}