#include<cstdio> 
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define fo(i,j,k) for(i=j;i<=k;i++)
#define fd(i,j,k) for(i=j;i>=k;i--)
#define cmax(a,b) (a=(a>b)?a:b)
#define cmin(a,b) (a=(a<b)?a:b)
typedef long long ll;
const int N=1e5+5,M=4e5+5,mo=1e9+7;
ll tr[M],a[N],b[N],s[N],n,K,i,j,k,L[N],R[N],v[M],m,f[N],ans;
void change(ll x,ll l,ll r,ll i,ll j,ll v)
{
	if (i>j) return ;
	if (l==i&&j==r)
	{
		tr[x]=v;
		return ;
	}
	if (tr[x]) tr[x*2]=tr[x*2+1]=tr[x],tr[x]=0;
	ll m=(l+r)/2;
	if (m>=j) change(x*2,l,m,i,j,v);
	else if (m<i) change(x*2+1,m+1,r,i,j,v);
	else change(x*2,l,m,i,m,v),change(x*2+1,m+1,r,m+1,j,v);
}
ll get(ll x,ll l,ll r,ll p)
{
	if (l==r) return tr[x];
	if (tr[x]) tr[x*2]=tr[x*2+1]=tr[x],tr[x]=0;
	ll m=(l+r)/2;
	if (m>=p) return get(x*2,l,m,p);else return get(x*2+1,m+1,r,p);
}
ll calc(ll x)
{
	ll y=get(1,1,m,x);
	if (!y) return 0;
	return f[y]+(v[L[y]]-v[x]+K)%K;
}
int main()
{
	scanf("%lld %lld\n",&n,&K);
	fo(i,1,n)
	{
		scanf("%lld %lld",a+i,b+i);
		s[i]=s[i-1]+a[i];
		if (b[i]==1&&2*a[i]>K)
		{
			printf("-1\n");
			return 0;
		}
	}
	fo(i,1,n)
		if (b[i]==1)
		{
			L[i]=((-2*s[i])%K+K)%K;
			R[i]=((-2*s[i-1])%K+K)%K;
			swap(L[i],R[i]);
		}else L[i]=0,R[i]=K-1;
	fo(i,1,n) v[++m]=L[i],v[++m]=R[i];
	sort(v+1,v+m+1);
	m=unique(v+1,v+m+1)-v-1;
	fd(i,n,1)
	{
		L[i]=lower_bound(v+1,v+m+1,L[i])-v;
		R[i]=lower_bound(v+1,v+m+1,R[i])-v;
		f[i]=calc(L[i]);
		if (L[i]>R[i]) 
			change(1,1,m,R[i]+1,L[i]-1,i);
		else change(1,1,m,1,L[i]-1,i),change(1,1,m,R[i]+1,m,i);
	}
	ans=f[1];
	fo(i,1,m) 
		ans=min(ans,calc(i));
	printf("%lld\n",s[n]*2+ans);
}