#include"bits/stdc++.h"


#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(int x = (l);x<=(u);x++)
#define RREP(x,l,u) for(int x = (l);x>=(u);x--)
#define mst(x,a) memset(x,a,sizeof(x))
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define se second
#define fi first
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define cl(x) x.clear()

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;

const int maxn = 200010;
const int mod = 1e9+7;
const int MAX = 1000000010;
const double eps = 1e-6;
const double PI = acos(-1);

template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);}
template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);}
template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);}
template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;}
template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;}

int n,K;

ll A[maxn],B[maxn],L[maxn],R[maxn];

int hs[maxn],cnt;

ll f[maxn],g[maxn],ans=1e18;

struct Node{
	int l,r,mn;
}T[maxn<<2];

void build(int i,int l,int r){
	T[i].l=l;T[i].r=r;T[i].mn=n+1;
	if(l==r)return;
	int M=l+r>>1;
	build(i<<1,l,M);build(i<<1|1,M+1,r);
}

int query(int i,int x){
	if(T[i].l==T[i].r)return T[i].mn;
	int ans=T[i].mn,M=T[i].l+T[i].r>>1;
	if(x<=M)ans=min(ans,query(i<<1,x));else ans=min(ans,query(i<<1|1,x));
	return ans;
}

void update(int i,int l,int r,int x){
	if(l>r)return;
	if(l<=T[i].l&&T[i].r<=r){
		T[i].mn=min(T[i].mn,x);
		return;
	}
	int M=T[i].l+T[i].r>>1;
	if(l<=M)update(i<<1,l,r,x);if(r>M)update(i<<1|1,l,r,x);
}

int c[maxn];

void add(int x,int y){
	for(;x<=cnt;x+=x&-x)c[x]+=y;
}
int query(int x){
	int ans=0;
	for(;x;x-=x&-x)ans+=c[x];
	return ans;
}

void Work(){
	int now=0;
	REP(i,1,n){
		if(B[i]==2)L[i]=0,R[i]=K-1;
		else{
			if(A[i]*2>K){
				puts("-1");
				return;
			}
			L[i]=now;R[i]=(now-A[i]*2+K)%K;
		}
		now=(now-(A[i]*2)%K+K)%K;
	}
	REP(i,1,n)hs[++cnt]=L[i],hs[++cnt]=R[i];
	sort(hs+1,hs+cnt+1);cnt=unique(hs+1,hs+cnt+1)-hs-1;
	REP(i,1,n)L[i]=LB(hs+1,hs+cnt+1,L[i])-hs,R[i]=LB(hs+1,hs+cnt+1,R[i])-hs;
	build(1,1,cnt);
	RREP(i,n,1){
		int t;
		t=query(1,L[i]);
		if(t!=n+1)f[i]=min(f[t]+(hs[L[t]]-hs[L[i]]+K)%K,g[t]+(hs[R[t]]-hs[L[i]]+K)%K);
		t=query(1,R[i]);
		if(t!=n+1)g[i]=min(f[t]+(hs[L[t]]-hs[R[i]]+K)%K,g[t]+(hs[R[t]]-hs[R[i]]+K)%K);
		if(L[i]<=R[i])update(1,1,L[i]-1,i),update(1,R[i]+1,cnt,i);
		else update(1,R[i]+1,L[i]-1,i);
	}
	REP(i,1,n){
		if(query(L[i])==i-1)ans=min(ans,f[i]);
		if(query(R[i])==i-1)ans=min(ans,g[i]);
		if(L[i]<=R[i])add(L[i],1),add(R[i]+1,-1);
		else add(1,1),add(R[i]+1,-1),add(L[i],1);
	}
	REP(i,1,n)ans+=A[i]*2;
	cout<<ans<<endl;
}

void Init(){
	read(n,K);
	REP(i,1,n)read(A[i],B[i]);
}

int main(){
	Init();
	Work();
	return 0;
}


Measured memory_bytes: 14976