#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100010
#define inf 1000000000000000000LL
#define ll long long
using namespace std;

struct aa{int l, r;}a[N];
struct bb{int x, f;}b[N];
struct tt{int l, r, f;}t[12000010];
int n, k, ff, now, m, L, R, root[N], l, sum, l1, x;
ll s[N], f1[N], ans, f[N];

inline void ins(int i, int a, int b, int L, int R){
	t[l]=t[i];
	if(a<=L&&R<=b){t[l].f++; return;}
	int mid=(L+R)>>1, l1=l;
	if(a<=mid){l++; t[l1].l=l; ins(t[i].l, a, b, L, mid);}
	if(mid<b){l++; t[l1].r=l; ins(t[i].r, a, b, mid+1, R);}
}
inline void cf(int x1, int x2, int x, int L, int R){
	if(!x1&&!x2)return;
	sum+=t[x1].f-t[x2].f;
	if(L==R)return;
	int mid=(L+R)>>1;
	if(x<=mid)cf(t[x1].l, t[x2].l, x, L, mid); else cf(t[x1].r, t[x2].r, x, mid+1, R);
}
inline int check(int x, int L, int R){
	sum=0; cf(root[R], root[L-1], x, 0, k);
	if(sum==R-L+1)return -1;
	int mid;
	while(L<R){
		mid=(L+R)>>1; sum=0;
		cf(root[mid], root[L-1], x, 0, k);
		if(sum==mid-L+1)L=mid+1; else R=mid;
	}
	return L;
}

int main(){
	scanf("%d%d", &n, &k); ff=1;
	for(int i=1; i<=n; i++){
		scanf("%d%d", &b[i].x, &b[i].f); if(b[i].f==2)ff=0;
		if(b[i].f==1&&b[i].x*2>k){printf("-1"); return 0;}
	}
	
	if(ff){
		s[0]=0; for(int i=1; i<=n; i++)s[i]=s[i-1]+b[i].x;
		f1[0]=0; now=0;
		for(int i=1; i<=n; i++){
			while((s[i]-s[now])*2>k)now++;
			if(!now)f1[i]=0; else f1[i]=f1[now]+k-(s[i]-s[now])*2;
		}
		ans=f1[n];
		for(int i=now; i<=n; i++)ans=min(ans, f1[i]);
		printf("%lld", s[n]*2+ans);
		return 0;
	}
	
	s[0]=0; for(int i=1; i<=n; i++)s[i]=s[i-1]+b[i].x;
	m=n; n=0;
	for(int i=1; i<=m; i++)if(b[i].f==1){
		R=-2*s[i]%k; if(R<0)R+=k;
		L=-2*s[i-1]%k; if(L<0)L+=k;
		n++; a[n].l=L; a[n].r=R;
	}
	
	l=root[0]=0; t[0].l=t[0].r=t[0].f=0;
	for(int i=1; i<=n; i++){
		root[i]=root[i-1];
		if(a[i].l<=a[i].r){l++; l1=l; ins(root[i], a[i].l, a[i].r, 0, k); root[i]=l1;}
		else{
			l++; l1=l; ins(root[i], 0, a[i].r, 0, k); root[i]=l1;
			l++; l1=l; ins(root[i], a[i].l, k-1, 0, k); root[i]=l1;
		}
	}
	f[n]=0;
	for(int i=n-1; i; i--){
		x=check(a[i].l, i+1, n);
		if(x==-1)f[i]=0;
		else{
			f[i]=inf;
			if(a[i].l<a[x].l)f[i]=min(f[i], a[x].l-a[i].l+f[x]);
			if(a[x].r<a[i].l)f[i]=min(f[i], a[x].l-a[i].l+k+f[x]);
		}
		/*
		f[i][0]=inf;
		for(int j=i+1; j<=n; j++){
			if(a[j].l<=a[j].r){
				if(a[j].r<a[i].l){f[i][0]=min(f[i][0], a[j].l-a[i].l+k+f[j][0]); break;}
				if(a[i].l<a[j].l){f[i][0]=min(f[i][0], a[j].l-a[i].l+f[j][0]); break;}
			}else{
				if(a[j].r<a[i].l&&a[i].l<a[j].l){f[i][0]=min(f[i][0], a[j].l-a[i].l+f[j][0]); break;}
			}
		}
		if(f[i][0]==inf)f[i][0]=0;
		f[i][1]=inf;
		for(int j=i+1; j<=n; j++){
			if(a[j].l<=a[j].r){
				if(a[j].r<a[i].r){f[i][1]=min(f[i][1], a[j].l-a[i].r+k+f[j][0]); break;}
				if(a[i].r<a[j].l){f[i][1]=min(f[i][1], a[j].l-a[i].r+f[j][0]); break;}
			}else{
				if(a[j].r<a[i].r&&a[i].r<a[j].l){f[i][1]=min(f[i][1], a[j].l-a[j].r+f[j][0]); break;}
			}
		}
		if(f[i][1]==inf)f[i][1]=0;
		*/
	}
	
	ans=inf;
	for(int i=1; i<=n; i++){
		x=check(a[i].l, 1, n);
		if(x==-1)ans=0;
		else{
			if(a[i].l<a[x].l)ans=min(ans, a[x].l-a[i].l+f[x]);
			if(a[x].r<a[i].l)ans=min(ans, a[x].l-a[i].l+k+f[x]);
		}
		x=check(a[i].r, 1, n);
		if(x==-1)ans=0;
		else{
			if(a[i].r<a[x].l)ans=min(ans, a[x].l-a[i].r+f[x]);
			if(a[x].r<a[i].r)ans=min(ans, a[x].l-a[i].r+k+f[x]);
		}
		/*
		ll ans1=inf;
		for(int j=1; j<=n; j++){
			if(a[j].l<=a[j].r){
				if(a[j].r<a[i].l){ans1=min(ans1, a[j].l-a[i].l+k+f[j][0]); break;}
				if(a[i].l<a[j].l){ans1=min(ans1, a[j].l-a[i].l+f[j][0]); break;}
			}else{
				if(a[j].r<a[i].l&&a[i].l<a[j].l){ans1=min(ans1, a[i].l-a[j].r+f[j][0]); break;}
			}
		}
		if(ans1==inf)ans1=0; ans=min(ans, ans1);
		ans1=inf;
		for(int j=1; j<=n; j++){
			if(a[j].l<=a[j].r){
				if(a[j].r<a[i].r){ans1=min(ans1, a[j].l-a[i].r+k+f[j][0]); break;}
				if(a[i].r<a[j].l){ans1=min(ans1, a[j].l-a[i].r+f[j][0]); break;}
			}else{
				if(a[j].r<a[i].r&&a[i].r<a[j].l){ans1=min(ans1, a[j].l-a[i].r+f[j][0]); break;}
			}
		}
		if(ans1==inf)ans1=0; ans=min(ans, ans1);
		*/
	}
	printf("%lld", s[m]*2+ans);
	return 0;
}

Measured memory_bytes: 86144