#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long int64;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e5+5;
int n, m, k, a[N], b[N], is_cov[N<<2], cov[N<<2], val[N<<2], _[N<<1];
void pushc(int x, int d) { is_cov[x] = 1, val[x] = cov[x] = d; }
void pushdown(int x) { if(is_cov[x]) pushc(x<<1, cov[x]), pushc(x<<1|1, cov[x]), is_cov[x] = 0; }
void cover(int x, int l, int r, int ql, int qr, int d)
{
    if(ql > qr) return; if(ql <= l&&r <= qr) return pushc(x, d);
    int mid = (l+r)>>1; pushdown(x);
    if(ql <= mid) cover(x<<1, l, mid, ql, qr, d);
    if(qr > mid) cover(x<<1|1, mid+1, r, ql, qr, d);
}
int query(int x, int l, int r, int p)
{
    if(l == r) return val[x];
    int mid = (l+r)>>1; pushdown(x);
    if(p <= mid) return query(x<<1, l, mid, p);
    else return query(x<<1|1, mid+1, r, p);
}
int sl[N], sr[N];
int64 f[N], s[N];
int64 query(int p) { int q = query(1, 1, m, p); return q?f[q]+(_[sl[q]]-_[p]+k)%k:0; }
int main()
{
    n = read(), k = read();
    for(int i = 1; i <= n; ++i)
    {
        a[i] = read(), b[i] = read(), s[i] = s[i-1]+a[i], sl[i] = 0, sr[i] = k-1; 
        if(b[i] != 2&&2*a[i] > k) return puts("-1"), 0;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(b[i] == 1) sl[i] = (-2*s[i-1]%k+k)%k, sr[i] = (-2*s[i]%k+k)%k;
        _[++m] = sl[i], _[++m] = sr[i];
    }
    sort(_+1, _+1+m), m = unique(_+1, _+1+m)-_-1;
    for(int i = n; i; --i)
    {
        sl[i] = lower_bound(_+1, _+1+m, sl[i])-_;
        sr[i] = lower_bound(_+1, _+1+m, sr[i])-_;
        f[i] = query(sl[i]);
        if(sl[i] > sr[i]) cover(1, 1, m, sr[i]+1, sl[i]-1, i);
        else cover(1, 1, m, 1, sl[i]-1, i), cover(1, 1, m, sr[i]+1, m, i);
    }
    int64 ans = f[1]; 
    for(int i = 1; i <= m; ++i) ans = min(ans, query(i));
    printf("%lld\n", ans+2*s[n]);
    return 0;
}