#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=4e5+5;
typedef long long ll;
#define lch x<<1
#define rch x<<1|1
ll tree[N];
ll a[N],b[N];
ll cnt,n,k;
ll sum[N];
ll t[N];
ll L[N],R[N];
ll dp[N];
void Pushdown(ll x){
    if(tree[x]){
        tree[lch]=tree[rch]=tree[x];
        tree[x]=0;
    }
}
void Modify(ll x,ll l,ll r,ll pl,ll pr,ll d){
    if(l>r||pl>pr)
        return ;
    if(pl<=l&&r<=pr){
        tree[x]=d;
        return ;
    }
    Pushdown(x);
    ll mid=(l+r)>>1;
    if(pl<=mid)
        Modify(lch,l,mid,pl,pr,d);
    if(pr>mid)
        Modify(rch,mid+1,r,pl,pr,d);
}
ll Query(ll x,ll l,ll r,ll pos){
    if(l==r)
        return tree[x];
    Pushdown(x);
    ll mid=(l+r)>>1;
    if(pos<=mid)
        return Query(lch,l,mid,pos);
    else
        return Query(rch,mid+1,r,pos);
}
ll Ask(ll pos){
    ll c=Query(1,1,cnt,pos);
    if(!c)
        return 0;
    ll res=1ll*dp[c]+(t[L[c]]-t[pos]+k)%k;
    return res;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(ll i=1;i<=n;i++){
        scanf("%d%d",&a[i],&b[i]);
        sum[i]=sum[i-1]+a[i];
        if(b[i]==1&&2*a[i]>k){
            puts("-1");
            return 0;
        }
    }
    for(ll i=1;i<=n;i++){
        if(b[i]==1){
            L[i]=(-2*sum[i-1]%k+k)%k;
            R[i]=(-2*sum[i]%k+k)%k;
        }
        else{
            L[i]=0;
            R[i]=k-1;
        }
        t[i*2-1]=L[i];
        t[i*2]=R[i];
    }
    sort(t+1,t+2*n+1);
    cnt=unique(t+1,t+2*n+1)-t-1;
    for(ll i=1;i<=n;i++){
        L[i]=lower_bound(t+1,t+cnt+1,L[i])-t,R[i]=lower_bound(t+1,t+cnt+1,R[i])-t;
    }
    for(ll i=n;i>0;i--){
        dp[i]=Ask(L[i]);
        if(L[i]>R[i]){
            Modify(1,1,cnt,R[i]+1,L[i]-1,i);
        }
        else{
            Modify(1,1,cnt,1,L[i]-1,i);
            Modify(1,1,cnt,R[i]+1,cnt,i);
        }
    }
    ll ans=dp[1];
    for(ll i=1;i<=cnt;i++){
        ans=min(ans,Ask(i));
        //printf("%lld\n",Ask(i));
    }
    printf("%lld\n",ans+sum[n]*2);
}
