#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

const int mod = (int) 1e9 + 7;

void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int mult(int x, int y) {
  return (long long) x * y % mod;
}

int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

void precalc() {
}


const int maxn = (int) 1e5 + 10;
int n, k;
int a[maxn], b[maxn];

int read() {
  if (scanf("%d%d", &n, &k) < 2) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", a + i, b + i);
  }
  return 1;
}

long long sums[maxn];


long long dp[2][maxn];

map<int, int> tree;

void addTree(int l, int r, int nval) {
  //eprintf("f[%d..%d) := %d\n", l, r, nval);
  auto iter = tree.upper_bound(l);
  --iter;
  int lval = iter->second;
  tree[l] = nval;
  while (1) {
    iter = tree.upper_bound(l);
    if (iter == tree.end() || iter->first > r) {
      tree[r] = lval;
      break;
    }
    if (iter->first == r) {
      break;
    }
    lval = iter->second;
    tree.erase(iter);
  }
}

void solve() {
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    res += a[i] * 2;
  }

  for (int iter = 0; iter < 2; ++iter) {
    sums[0] = 0;
    for (int i = 0; i < n; ++i) {
      sums[i + 1] = sums[i] + a[i];
    }

    auto *d = dp[iter];
    d[n] = 0;

    tree.clear();
    tree[0] = -1;
    for (int i = n - 1; i >= 0; --i) {
      if (b[i] == 1 && a[i] > k / 2) {
        printf("-1\n");
        return;
      }
      //eprintf("i = %d\n", i);

      long long best = 1e18;
      int bpos = -1;
      for (int j = 0; j < 2; ++j) {
        long long s0 = sums[i] + (!j ? 0 : (k / 2));
        //eprintf("s0 = %lld\n", s0);
        int rem = s0 % k;
        auto iter = tree.upper_bound(rem);
        --iter;
        int pos = iter->second;

        if (pos != -1) {
          long long diff = sums[pos] - s0;
          int rem = diff % k;
          if (rem < 0) {
            rem += k;
          }
          if (rem) {
            diff += k - rem;
          }
          assert(diff >= 0ll && diff % k == 0);

          diff /= k;
          diff *= 2;
          diff += j;
          if (best > diff) {
            best = diff;
            bpos = pos;
          }
        }
      }
      if (bpos == -1) {
        d[i] = 0;
      } else {
        //eprintf("best = %lld\n", best);
        d[i] = best * k - 2 * (sums[bpos] - sums[i]) + d[bpos];
      }
      //eprintf("d[%d] = %lld\n", i, d[i]);

      if (b[i] == 1) {
        int l = sums[i] % k;
        int len = min(k, a[i]);
        addTree(l, min(l + len, k), i);
        if (l + len > k) {
          addTree(0, l + len - k, i);
        }
      }
    }
    reverse(a, a + n);
    reverse(b, b + n);
  }
  long long best = 1e18;
  for (int i = 0; i <= n; ++i) {
    best = min(best, dp[0][i] + dp[1][n - i]);
  }
  res += best;
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 8064