// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 1e5 + 5, LOGN = 19;

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

void input()
{
    read(N); read(K);
    for (int i = 1; i <= N; ++i) {
        read(A[i]); read(B[i]);
    }
}

namespace SEGT
{

const int SIZE = (1 << 19) + 5;

int modt[SIZE];

#define LC (u << 1)
#define RC (u << 1 | 1)

void pushdown(int u)
{
    if (modt[u]) {
        modt[LC] = modt[u];
        modt[RC] = modt[u];
        modt[u] = 0;
    }
}

void modify(int u, int l, int r, int ql, int qr, int x)
{
    if (ql <= l && r <= qr) {
        modt[u] = x;
        return;
    }
    pushdown(u);
    int mid = (l + r) >> 1;
    if (ql <= mid)
        modify(LC, l, mid, ql, qr, x);
    if (mid < qr)
        modify(RC, mid + 1, r, ql, qr, x);
}

int get(int u, int l, int r, int p)
{
    if (modt[u])
        return modt[u];
    assert(l < r);
    int mid = (l + r) >> 1;
    if (p <= mid)
        return get(LC, l, mid, p);
    else
        return get(RC, mid + 1, r, p);
}

}

int L[MAXN], R[MAXN];
int hash[MAXN * 2 + 5];
int hashsize;
LL dpL[MAXN], dpR[MAXN];

LL getnext(int p)
{
    int t = SEGT::get(1, 1, hashsize, p);
    if (!~t)
        return 0;
    return std::min((hash[L[t]] - hash[p] + K) % K + dpL[t], (hash[R[t]] - hash[p] + K) % K + dpR[t]);
}

void contrib(int l, int r, int x)
{
    if (l <= r) {
        if (1 <= l - 1)
            SEGT::modify(1, 1, hashsize, 1, l - 1, x);
        if (r + 1 <= hashsize)
            SEGT::modify(1, 1, hashsize, r + 1, hashsize, x);
    } else {
        if (r + 1 <= l - 1)
            SEGT::modify(1, 1, hashsize, r + 1, l - 1, x);
    }
}

void solve()
{
    for (int i = 1; i <= N; ++i) {
        if (B[i] == 1 && 2 * A[i] > K) {
            puts("-1");
            return;
        }
    }

    LL sumA = 0;
    for (int i = 1; i <= N; ++i) {
        if (B[i] == 1) {
            L[i] = (K - 2 * sumA % K) % K;
            R[i] = (K - 2 * (sumA + A[i]) % K) % K;
        } else {
            L[i] = 0;
            R[i] = K - 1;
        }
        sumA += A[i];
        // printf("[%d,%d]\n", L[i], R[i]);
    }

    hash[++hashsize] = 0;
    hash[++hashsize] = K - 1;
    for (int i = 1; i <= N; ++i) {
        hash[++hashsize] = L[i];
        hash[++hashsize] = R[i];
    }
    std::sort(hash + 1, hash + hashsize + 1);
    hashsize = std::unique(hash + 1, hash + hashsize + 1) - hash - 1;
    for (int i = 1; i <= N; ++i) {
        L[i] = std::lower_bound(hash + 1, hash + hashsize + 1, L[i]) - hash;
        R[i] = std::lower_bound(hash + 1, hash + hashsize + 1, R[i]) - hash;
    }

    SEGT::modify(1, 1, hashsize, 1, hashsize, -1);
    for (int i = N; i >= 1; --i) {
        dpL[i] = getnext(L[i]);
        dpR[i] = getnext(R[i]);
        contrib(L[i], R[i], i);
    }

    LL ans = 1e18;
    SEGT::modify(1, 1, hashsize, 1, hashsize, -1);
    for (int i = 1; i <= N; ++i) {
        if (!~SEGT::get(1, 1, hashsize, L[i]))
            chkmin(ans, dpL[i]);
        if (!~SEGT::get(1, 1, hashsize, R[i]))
            chkmin(ans, dpR[i]);
        contrib(L[i], R[i], i);
    }
    ans += sumA * 2;

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

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 高度的幽默往往源自高度的严肃。
// 说幽默的人灵光一闪，绣口一开，
// 听幽默的人反应也要敏捷，才能接个正著。
// 这种场合，听者的悟性接近禅的顿悟。
//   --《谈幽默》余光中


Measured memory_bytes: 5248