#include<iostream>
#include<algorithm>
#include<map>
#define int long long
using namespace std;
const int N=4e5+10;
map<int,int> Map;
int n,k,a[N],t[N],f[N],A[N],B[N],L[N],R[N],cc,ans=1e18,tt,rk[N];
void pushdown(int x) {if(t[x]) t[x<<1]=t[x<<1|1]=t[x],t[x]=0;}
void Cover(int x,int l,int r,int gl,int gr,int w)
{
	if(l>r||gl>gr) return;
	if(l>=gl&&r<=gr) {t[x]=w;return;}
	int mid=(l+r)>>1;pushdown(x);
	if(gl<=mid) Cover(x<<1,l,mid,gl,gr,w);
	if(gr>mid) Cover(x<<1|1,mid+1,r,gl,gr,w);
}
int Query(int x,int l,int r,int p)
{
	if(l==r) return t[x];
	int mid=(l+r)>>1;pushdown(x);
	return p<=mid?Query(x<<1,l,mid,p):Query(x<<1|1,mid+1,r,p);
}
int query(int x)
{
	int d=Query(1,1,tt,x);
	return !d?0:f[d]+((rk[L[d]]-rk[x])%k+k)%k;
}
signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>B[i],A[i]=A[i-1]+a[i];
		if(B[i]==1) L[++cc]=(-2*A[i-1]%k+k)%k,R[cc]=(-2*A[i]%k+k)%k;
		if(B[i]==1&&2*a[i]>k) return puts("-1"),0;
	}
	for(int i=1;i<=cc;i++) B[++tt]=L[i],B[++tt]=R[i];
	sort(B+1,B+tt+1);tt=unique(B+1,B+tt+1)-B-1;
	for(int i=1;i<=tt;i++) rk[i]=B[i],Map[B[i]]=i;
	for(int i=1;i<=cc;i++) L[i]=Map[L[i]],R[i]=Map[R[i]];
	for(int i=cc;i>=1;i--)
	{
		f[i]=query(L[i]);
		if(L[i]>R[i]) Cover(1,1,tt,R[i]+1,L[i]-1,i);
		else Cover(1,1,tt,1,L[i]-1,i),Cover(1,1,tt,R[i]+1,tt,i);
	}
	for(int i=1;i<=tt;i++) ans=min(ans,query(i));
	cout<<ans+2*A[n]<<endl;
}
