#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#define N (1000010)
#define P ()
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1{
	inline char read(){
		static const int IN_LEN=1000000;
		static char buf[IN_LEN],*s,*t;
		return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
	}
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=read();!isdigit(c);c=read()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
	inline char readc(char &c){
		for(c=read();!isalpha(c)&&!isdigit(c);c=read())
		if(c==-1)return 0;
	}
	const int OUT_LEN = 10000000;
	char obuf[OUT_LEN],*ooh=obuf;
	inline void print(char c) {
		if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
		*ooh++=c;
	}
	template<class T>
	inline void print(T x){
		static int buf[30],cnt;
		if(x==0)print('0');
		else{
			if(x<0)print('-'),x=-x;
			for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
			while(cnt)print((char)buf[cnt--]);
		}
	}
	inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}
namespace fastIO2{
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=getchar();!isdigit(c);c=getchar()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=getchar())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
}
using namespace fastIO1;
#define int long long
int n,m,cnt,a[N],tp[N],ls[N],t[N<<2],L[N],R[N]; LL f[N],sum[N];
void pushdown(int x){if(t[x])t[x*2]=t[x*2+1]=t[x],t[x]=0;}
void modify(int L,int R,int l,int r,int v,int x){
	if(l>r)return;
	if(L==l&&R==r){t[x]=v;return;}
	pushdown(x);
	int mid=(L+R)>>1;
	if(r<=mid)modify(L,mid,l,r,v,x*2);
	else if(l>mid)modify(mid+1,R,l,r,v,x*2+1);
	else modify(L,mid,l,mid,v,x*2),modify(mid+1,R,mid+1,r,v,x*2+1);
}
int query(int L,int R,int p,int x){
	if(L==R)return t[x];
	pushdown(x);
	int mid=(L+R)>>1;
	if(p<=mid)return query(L,mid,p,x*2);
	else return query(mid+1,R,p,x*2+1);
}
LL getval(int x){
	int k=query(1,cnt,x,1);
	return k?(f[k]+(ls[L[k]]-ls[x]+m)%m):0;
}
signed main(){
	read(n),read(m);
	for(int i=1;i<=n;i++){
		read(a[i]),read(tp[i]),sum[i]=sum[i-1]+a[i];
		if(tp[i]==2)continue; if(2*a[i]>m){puts("-1"),exit(0);}
	}
	for(int i=1;i<=n;i++){
		if(tp[i]==1){
			L[i]=(-2ll*sum[i-1]%m+m)%m,
			R[i]=(-2ll*sum[i]%m+m)%m;
		}
		else L[i]=0,R[i]=m-1;
		ls[++cnt]=L[i],ls[++cnt]=R[i];
	}
	sort(ls+1,ls+cnt+1),cnt=unique(ls+1,ls+cnt+1)-ls-1;
	for(int i=n;i;i--){
//		cout<<L[i]<<" "<<R[i]<<endl;
		L[i]=lower_bound(ls+1,ls+cnt+1,L[i])-ls;
		R[i]=lower_bound(ls+1,ls+cnt+1,R[i])-ls;
//		cout<<L[i]<<" "<<R[i]<<endl;
		f[i]=getval(L[i]);
//		cout<<i<<" "<<f[i]<<endl;
		if(L[i]>R[i])modify(1,cnt,R[i]+1,L[i]-1,i,1);
		else modify(1,cnt,1,L[i]-1,i,1),modify(1,cnt,R[i]+1,cnt,i,1);
	}
	LL ans=f[1];
	for(int i=1;i<=cnt;i++)ans=min(ans,getval(i));
	cout<<(ans+sum[n]+sum[n]);
}

Measured memory_bytes: 28032