#include <bits/stdc++.h>
using namespace std;
int n, k, tot, b[100010], L[100010], R[100010], lsh[200010], tr[800010];
long long ans, dp[100010], sum[100010];
int Minus(int a, int b) { return a < b ? a - b + k : a - b; }
int query(int o, int l, int r, int x) {
  if (l == r) return tr[o];
  if (tr[o]) {
    tr[o << 1] = tr[o << 1 | 1] = tr[o];
    tr[o] = 0;
  }
  int mid = (l + r) >> 1;
  if (x <= mid) return query(o << 1, l, mid, x);
  return query(o << 1 | 1, mid + 1, r, x);
}
void update(int o, int l, int r, int L, int R, int v) {
  if (L > R) return;
  if (L <= l && r <= R) {
    tr[o] = v;
    return;
  }
  if (tr[o]) {
    tr[o << 1] = tr[o << 1 | 1] = tr[o];
    tr[o] = 0;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) update(o << 1, l, mid, L, R, v);
  if (R > mid) update(o << 1 | 1, mid + 1, r, L, R, v);
  return;
}
void Getans(int o, int l, int r) {
  if (l == r) {
    if (!tr[o]) {
      printf("%lld", 2ll * sum[n]);
      exit(0);
    }
    ans = min(ans, dp[tr[o]] + Minus(lsh[L[tr[o]]], lsh[l]));
    return;
  }
  if (tr[o]) {
    tr[o << 1] = tr[o << 1 | 1] = tr[o];
    tr[o] = 0;
  }
  int mid = (l + r) >> 1;
  Getans(o << 1, l, mid);
  Getans(o << 1 | 1, mid + 1, r);
  return;
}
int main() {
  scanf("%d%d", &n, &k);
  lsh[++tot] = 0;
  lsh[++tot] = k - 1;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d%d", &a, b + i);
    sum[i] = sum[i - 1] + a;
    if (b[i] == 1 && (a << 1) > k) {
      puts("-1");
      return 0;
    }
    if (b[i] == 1) {
      L[i] = sum[i - 1] % k * (k - 2ll) % k;
      R[i] = sum[i] % k * (k - 2ll) % k;
      lsh[++tot] = L[i];
      lsh[++tot] = R[i];
    } else
      L[i] = 0, R[i] = k - 1;
  }
  sort(lsh + 1, lsh + tot + 1);
  tot = unique(lsh + 1, lsh + tot + 1) - (lsh + 1);
  for (int i = n; i >= 1; i--) {
    L[i] = lower_bound(lsh + 1, lsh + tot + 1, L[i]) - lsh;
    R[i] = lower_bound(lsh + 1, lsh + tot + 1, R[i]) - lsh;
    int tmp = query(1, 1, tot, L[i]);
    if (!tmp)
      dp[i] = 0;
    else
      dp[i] = dp[tmp] + Minus(lsh[L[tmp]], lsh[L[i]]);
    if (L[i] > R[i])
      update(1, 1, tot, R[i] + 1, L[i] - 1, i);
    else
      update(1, 1, tot, 1, L[i] - 1, i), update(1, 1, tot, R[i] + 1, tot, i);
  }
  ans = dp[1];
  Getans(1, 1, tot);
  printf("%lld", ans + 2 * sum[n]);
  return 0;
}
