#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
typedef long long LL;
const int N=2e5+100;
//这题太神!
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
int L[N],R[N],x[N];
int e[N<<2];
LL f[N],g[N];
#define lc (i<<1)
#define rc (lc|1)
inline void build(int i,int l,int r,int t) {
	e[i]=t;
	if (l!=r) {
		int mid=(l+r)>>1;
		build(lc,l,mid,t);
		build(rc,mid+1,r,t);
	}
}
inline void modify(int i,int l,int r,int L,int R,int k) {
	if (L<=l&&r<=R) e[i]=k;
	else {
		int mid=(l+r)>>1;
		if (L<=mid) modify(lc,l,mid,L,R,k);
		if (mid<R) modify(rc,mid+1,r,L,R,k);
	}
}
inline int query(int i,int l,int r,int k) {
	if (l==r) return e[i];
	int mid=(l+r)>>1;
	return min(e[i],k<=mid?query(lc,l,mid,k):query(rc,mid+1,r,k));
}
inline void add(int i,int l,int r,int L,int R) {
	if (L<=l&&r<=R) e[i]++;
	else {
		int mid=(l+r)>>1;
		if (L<=mid) add(lc,l,mid,L,R);
		if (mid<R) add(rc,mid+1,r,L,R);
	}
}
inline int Sum(int i,int l,int r,int k) {
	if (l==r) return e[i];
	int mid=(l+r)>>1;
	return e[i]+(k<=mid?Sum(lc,l,mid,k):Sum(rc,mid+1,r,k));
}
int main()
{
	int n=gi(),m=gi(),i,a,k,tp,sum=0,len=0;
	LL ans=1LL<<60,S=0;
	for (i=1;i<=n;i++) {
		a=gi(),tp=gi();S+=a<<1;
		if (tp==1&&a*2>m) return puts("-1"),0;
		if (tp==2) L[i]=0,R[i]=m-1;
		else L[i]=sum,R[i]=(sum-a*2%m+m)%m;
		sum=(sum-a*2%m+m)%m;
		x[++len]=L[i];
		x[++len]=R[i];
	}
	sort(x+1,x+1+len);
	len=unique(x+1,x+1+len)-x-1;
	build(1,1,len,n+1);
	for (i=n;i;i--) {
		L[i]=lower_bound(x+1,x+1+len,L[i])-x;
		R[i]=lower_bound(x+1,x+1+len,R[i])-x;

		k=query(1,1,len,L[i]);
		if (k!=n+1) f[i]=f[k]+(x[L[k]]-x[L[i]]+m)%m;
		k=query(1,1,len,R[i]);
		if (k!=n+1) g[i]=f[k]+(x[L[k]]-x[R[i]]+m)%m;

		if (L[i]<=R[i]) {
			if (R[i]<len) modify(1,1,len,R[i]+1,len,i);
			if (L[i]>1) modify(1,1,len,1,L[i]-1,i);
		} else if (R[i]+1<L[i]) modify(1,1,len,R[i]+1,L[i]-1,i);
	}
	build(1,1,len,0);
	for (i=1;i<=n;i++) {
		if (L[i]<=R[i]) add(1,1,len,L[i],R[i]);
		else add(1,1,len,L[i],len),add(1,1,len,1,R[i]);
		if (Sum(1,1,len,R[i])==i)
			ans=min(ans,g[i]);
	}
	cout<<ans+S<<endl;
	return 0;
}
