// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAXN=200005;



int segsiz;

int mn[MAXN*4];



int Query(int pos,int L=1,int R=segsiz,int id=1)

{

    if(L==R)

        return mn[id];

    int ret=mn[id],mid=(L+R)/2;

    if(pos<=mid)

        return min(ret,Query(pos,L,mid,id*2));

    return min(ret,Query(pos,mid+1,R,id*2+1));

}

void Insert(int l,int r,int v,int L=1,int R=segsiz,int id=1)

{

    if(l>r)

        return;

    if(l<=L&&R<=r)

    {

        mn[id]=min(mn[id],v);

        return;

    }

    int mid=(L+R)/2;

    if(l<=mid)

        Insert(l,r,v,L,mid,id*2);

    if(mid<r)

        Insert(l,r,v,mid+1,R,id*2+1);

}



int N,M,K,A[MAXN],B[MAXN];

int sumA[MAXN];

int L[MAXN],R[MAXN];

int val[MAXN];

long long dp[MAXN];



int main()

{

    scanf("%d%d",&N,&K);

    long long sum=0;

    for(int i=1;i<=N;i++)

    {

        scanf("%d%d",&A[i],&B[i]);

        if(B[i]==1&&2*A[i]>K)

        {

            puts("-1");

            return 0;

        }

        sum+=A[i];

        sumA[i]=(sumA[i-1]+A[i])%K;

    }

    for(int i=1;i<=N;i++)

    {

        if(B[i]==1)

        {

            L[i]=2*(K-sumA[i-1])%K;

            R[i]=2*(K-sumA[i])%K;

        }

        else

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

    }

    for(int i=1;i<=N;i++)

        val[i*2-1]=L[i],val[i*2]=R[i];

    sort(val+1,val+2*N+1);

    M=unique(val+1,val+2*N+1)-val-1;

    for(int i=1;i<=N;i++)

    {

        L[i]=lower_bound(val+1,val+M+1,L[i])-val;

        R[i]=lower_bound(val+1,val+M+1,R[i])-val;

    }

    segsiz=M;

    memset(mn,0x3F,sizeof mn);

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

    {

        int j=Query(L[i]);

        if(j==0x3F3F3F3F)

            dp[i]=0;

        else

            dp[i]=dp[j]+(val[L[j]]-val[L[i]]+K)%K;

        if(L[i]<=R[i])

        {

            Insert(1,L[i]-1,i);

            Insert(R[i]+1,M,i);

        }

        else

            Insert(R[i]+1,L[i]-1,i);

    }

    long long ans=0x3F3F3F3F3F3F3F3FLL;

    for(int i=1;i<=M;i++)

    {

        int j=Query(i);

        if(j==0x3F3F3F3F)

            ans=0;

        else

            ans=min(ans,1LL*(val[L[j]]-val[i]+K)%K+dp[j]);

    }

    ans=ans+2LL*sum;

    printf("%lld\n",ans);



    return 0;

}


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

using namespace std;



const int Maxn = 100005;

struct node {

  long long l, r, x, d;

  node(long long l, long long r, long long x, long long d) : 

    l(l), r(r), x(x), d(d) { }

  node() { l = r = x = d = 0; }

  bool operator<(const node &b) const {

    return l < b.l;

  }

};

int n, b[Maxn];

long long k, ans;

long long a[Maxn];

set <node> st;

int main() {

  scanf("%d %lld", &n, &k), ans = 0;

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

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

    ans += (a[i] <<= 1);

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

      return puts("-1") & 0;

    if (b[i] == 2) a[i] %= k;

  } st.insert({0, k, 0, 0});

  long long cur = 0;

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

    long long d = a[i];

    long long nx = cur + d;

    (nx > k) && (nx -= k);

    if (b[i] == 1) {

      if (cur < nx) {

        auto it = st.lower_bound({cur, 0, 0, 0});

        while (it -> r < nx) st.erase(it ++);

        node x1 = *it, x2 = *it;

        st.erase(it), x2.l = nx;

        long long now = x2.x + x2.d * (x2.r - nx);

        x1 = node(cur, nx, now, 1);

        st.insert(x1);

        if (x2.l ^ x2.r) st.insert(x2);

      } else {

        auto it = st.lower_bound({cur, 0, 0, 0});

        while (it != st.end()) st.erase(it ++);

        it = st.begin();

        while (it -> r < nx) st.erase(it ++);

        node x1 = *it, x2 = *it;

        st.erase(it), x2.l = nx;

        long long now = x2.x + x2.d * (x2.r - nx);

        x1 = node(0, nx, now, 1);

        st.insert(x1);

        if (x2.l != x2.r) st.insert(x2);

        now = x1.x + x1.d * x1.r;

        x2 = node(cur, k, now, 1);

        st.insert(x2);

      } cur = (cur + a[i]) % k;

    } else {

      (cur += a[i]) %= k;

      auto it = st.lower_bound({cur, 0, 0, 0});

      if (it == st.begin()) continue;

      if ((-- it) -> r <= cur) continue;

      node x1 = *it, x2 = *it;

      st.erase(it), x2.l = cur;

      x1.x = x2.x + x2.d * (x2.r - cur);

      x1.r = cur;

      st.insert(x1), st.insert(x2);

    }

  }

  long long mn = st.begin() -> x;

  for (auto x: st)

    mn = min(mn, x.x);

  printf("%lld\n", ans + mn);

  return 0;

}