#include <bits/stdc++.h>
#define maxn 200010
#define ms(x,y) memset(x,y,sizeof x)
#define inf 0x3f3f3f3f
#define fir first
#define sec second
#define lb(x) (x&(-x))
const int mod=1e9+7;
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
int mn[maxn<<2];
void modify(int p,int L,int R,int l,int r,int v){
	if(L==l&&r==R)return (void)(mn[p]=v);
	int mid=L+R>>1;
	if(r<=mid)modify(p<<1,L,mid,l,r,v);
	else if(l>mid)modify(p<<1|1,mid+1,R,l,r,v);
	else modify(p<<1,L,mid,l,mid,v),modify(p<<1|1,mid+1,R,mid+1,r,v);
//	mn[p]=min(mn[p<<1],mn[p<<1|1]);
}
int query(int p,int L,int R,int pos){
	if(L==R)return mn[p];
	int mid=L+R>>1;
	if(pos<=mid)return min(mn[p],query(p<<1,L,mid,pos));
	else if(pos>mid)return min(mn[p],query(p<<1|1,mid+1,R,pos));
}
int n,k;
int l[maxn],r[maxn],sum[maxn],a[maxn],b[maxn],hs[maxn],tot;
ll f[maxn];
int main(){
//	freopen("train.in", "r", stdin);
//    freopen("train.out", "w", stdout);
	ms(mn,inf);
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		sum[i]=((sum[i-1]-2*a[i])%k+k)%k;
		if(b[i]==1&&a[i]*2>k){
			puts("-1");
			return 0;
		}
		if(b[i]==2)l[i]=0,r[i]=k-1;
		else l[i]=sum[i-1],r[i]=sum[i];
		hs[++tot]=l[i],hs[++tot]=r[i];
	}
	sort(hs+1,hs+1+tot);
	tot=unique(hs+1,hs+1+tot)-hs-1;
	for(int i=1;i<=n;i++){
		l[i]=lower_bound(hs+1,hs+tot+1,l[i])-hs;
		r[i]=lower_bound(hs+1,hs+tot+1,r[i])-hs;	
	}
	for(int i=n;i;i--){
		int p=query(1,1,tot,l[i]);
		if(p!=inf){
			f[i]=f[p]+(hs[l[p]]-hs[l[i]]+k)%k;
		}
		if(l[i]<=r[i]){
			if(r[i]<tot)modify(1,1,tot,r[i]+1,tot,i);
			if(l[i]>1)modify(1,1,tot,1,l[i]-1,i);
		}else{
			if(r[i]+1<=l[i]-1)modify(1,1,tot,r[i]+1,l[i]-1,i);
		}
	}
	ll res=8e18;
	for(int i=1;i<=tot;i++){
		int p=query(1,1,tot,i);
		if(p!=inf)res=min(res,1ll*f[p]+(hs[l[p]]-hs[i]+k)%k);
		else res=0;
	}
	for(int i=1;i<=n;i++)res+=a[i]*2;
	cout<<res<<endl;
	return 0;
}