// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

//Coded in Dongge Jifang 4



int n,k,N;

long long a[100005],b[100005];

long long qzh[100005];

long long liml[100005],limr[100005];

long long dp[100005],dp2[100005];

long long ans=0x3f3f3f3f3f3f3f3fLL;



struct Node{

	long long num;

	Node *l,*r;

	Node(){

		num=n+1;

		l=r=0;

	}

}*root,*root2;



struct segtree{

	void init(Node* &now){

		if(!now){

			return;

		}

		init(now->l);

		init(now->r);

		delete now;

		now=0;

	}

	void update(int l,int r,long long x,Node* &now,int nl=0,int nr=k-1){

		if(!now){

			now=new Node();

		}

		if(nl>=l&&nr<=r){

			now->num=min(now->num,x);

			return;

		}

		if(nl>r||nr<l){

			return;

		}

		int m=(nl+nr)>>1;

		update(l,r,x,now->l,nl,m);

		update(l,r,x,now->r,m+1,nr);

		//now->num=min(now->l->num,now->r->num);

	}

	void upd(int l,int r,long long x,Node* &now){

		l=(l%k+k)%k;

		r=(r%k+k)%k;

		if(l<=r){

			update(l,r,x,now);

		}else{

			update(l,k-1,x,now);

			update(0,r,x,now);

		}

	}

	long long qmin(int whr,Node* &now,int nl=0,int nr=k-1){

		if(!now){

			now=new Node();

		}

		if(nl==whr&&nr==whr){

			return now->num;

		}

		int m=(nl+nr)>>1;

		if(m>=whr){

			return min(now->num,qmin(whr,now->l,nl,m));

		}else{

			return min(now->num,qmin(whr,now->r,m+1,nr));

		}

	}

}seg1,seg2;



int main(){

	scanf("%d%d",&n,&k);

	for(int i=1;i<=n;i++){

		scanf("%lld%lld",a+i,b+i);

		if(b[i]==1&&a[i]*2>k){

			puts("-1");

			return 0;

		}

	}

	for(int i=1;i<=n;i++){

		qzh[i]=qzh[i-1]+a[i];

	}

	for(int i=1;i<=n;i++){

		if(b[i]==1){

			liml[N]=2*(k-qzh[i]%k)%k;

			limr[N]=2*(k-qzh[i-1]%k)%k;

			N++;

		}

	}

	seg1.init(root);

	seg2.init(root2);

	dp[N-1]=dp2[N-1]=0;

	seg1.upd(liml[N-1]+1,limr[N-1]-1,N-1,root);

	seg2.upd(liml[N-1]+1,limr[N-1]-1,N-1,root2);

	//cerr<<liml[N-1]<<' '<<limr[N-1]<<endl;

	for(int i=N-2;i>=0;i--){

		int x=seg1.qmin(liml[i],root);

		//cerr<<liml[i]<<' '<<' '<<x<<endl;

		if(x==n+1){

			dp[i]=0;

		}else{

			dp[i]=min(dp[x]+((liml[x]-liml[i])%k+k)%k,dp2[x]+((limr[x]-liml[i])%k+k)%k);

		}

		x=seg2.qmin(limr[i],root2);

		if(x==n+1){

			dp2[i]=0;

		}else{

			dp2[i]=min(dp2[x]+((limr[x]-limr[i])%k+k)%k,dp[x]+((liml[x]-limr[i])%k+k)%k);

		}

		seg1.upd(liml[i]+1,limr[i]-1,i,root);

		seg2.upd(liml[i]+1,limr[i]-1,i,root2);

	}

	seg1.init(root);

	seg2.init(root2);

	for(int i=0;i<N;i++){

		int x=seg1.qmin(liml[i],root);

		//cerr<<i<<' '<<x<<' '<<n<<' '<<dp[i]<<endl;

		if(x==n+1){

			ans=min(ans,dp[i]);

		}

		x=seg2.qmin(limr[i],root2);

		if(x==n+1){

			ans=min(ans,dp2[i]);

		}

		seg1.upd(liml[i]+1,limr[i]-1,i,root);

		seg2.upd(liml[i]+1,limr[i]-1,i,root2);

	}

	printf("%lld\n",ans+qzh[n]*2);

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.107369589500942x):
#include<bits/stdc++.h>

using namespace std;

//14719021122270100109

typedef long long ll;

typedef pair<int,int> pi;

typedef vector<int> vi;

typedef unsigned long long ull;

typedef long double ldb;



#define f first

#define s second

#define all(x) (x).begin(), (x).end()

#define pb push_back

#define mp make_pair

#define forn(i, n) for(register int i=0; i<n; i++)

#define Forn(i, n) for(register int i=1; i<=n; i++)

#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)

#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)

#define mid l+r>>1

#define lid (id<<1|1)

#define rid ((id<<1)+2)

#define lson lid, l, mid

#define rson rid, mid, r



inline int in()

{

    char c=getchar();

    int neg=1, x=0;

    while(!isdigit(c)) (c=='-')?neg=-1, c=getchar():c=getchar();

    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48), c=getchar();

    return neg*x;

}

inline int lcm(int a, int b){return a*b/__gcd(a, b);}



const int MAX_T=(8e5+5);

const int MAX=1e5+5;



map<ll,int> MP;

int N, ptr, a[MAX], tp[MAX];

ll K, A[MAX], dp[MAX], L[MAX], R[MAX];



inline void re()

{

    puts("-1");

    exit(0);

}



class Seg_Tree

{

    private:

    int dat[MAX_T];

    inline void pushdown(int id)

    {

        if(!dat[id]) return;

        dat[lid]=dat[rid]=dat[id];

        dat[id]=0;

    }

    public:

    inline int get(int id, int l, int r, int a, int b)

    {

        if(r<=a||b<=l) return 0;

//        cout<<l<<" "<<r<<" "<<p<<"<< "<<dat[id]<<endl;

        if(a<=l&&r<=b) return dat[id];

        pushdown(id);

        return max(get(lson, a, b), get(rson, a, b));

    }

    inline void update(int id, int l, int r, int a, int b, int v)

    {

        if(r<=a||b<=l) return;

        if(a<=l&&r<=b)

        {

//            cout<<l<<" "<<r<<" "<<v<<"EE"<<endl;

            dat[id]=v;

            return;

        }

        pushdown(id);

        update(lson, a, b, v);

        update(rson, a, b, v);

    }

}S;



inline ll query(int x, ll v)

{

    int id=S.get(0, 0, ptr, x, x+1);

//    cout<<" "<<x<<" "<<id<<endl;

    if(!id) return 0;

//    cout<<"id="<<id<<endl;

//    cout<<dp[id]<<" "<<L[id]<<" "<<v<<endl;

//    cout<<"dp"<<dp[id]<<" "<<L[id]<<" "<<L[x]<<endl;

    return (dp[id]+(L[id]-v+K)%K);

}



int main()

{

    N=in(), K=in();

    Forn(i, N)

    {

        a[i]=in(), tp[i]=in()-1;

        A[i]=A[i-1]+a[i];

        if(tp[i]) continue;

        if(a[i]*2>K) re();

    }

    Forn(i, N)

    {

        if(!tp[i])

        {

            L[i]=(-2ll*A[i-1]%K+K)%K;

            R[i]=(-2ll*A[i]%K+K)%K;

        }

        else L[i]=0, R[i]=K-1;

        MP[L[i]]=0, MP[R[i]]=0;

    }

    foreach(it, MP) it->s=ptr++;

    

    for(int i=N; i>0; i--)

    {

        int l=MP[L[i]], r=MP[R[i]];

        dp[i]=query(l, L[i]);

//    cout<<":)"<<endl;

        if(l>r) S.update(0, 0, ptr, r+1, l, i);

        else    S.update(0, 0, ptr, 0, l, i), S.update(0, 0, ptr, r+1, ptr, i);

//        cout<<i<<" "<<dp[i]<<" m "<<l<<" "<<r<<" "<<L[i]<<" "<<R[i]<<endl;

//        cout<<i<<" "<<dp[i]<<endl;

    }

    ll ret=dp[1];

    foreach(it, MP) ret=min(ret, query(it->s, it->f));

    cout<<ret+A[N]*2ll<<endl;

    return 0;

}