#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 200005;
const LL inf = 1LL << 60;

int n, m, sum, tot, a[N], l[N], r[N], all[N], opt[N], val[N << 2];
LL ans, f[N], g[N];

inline void Build(int x, int l, int r, int v) {
  val[x] = v;
  if (l == r) {
    return ;
  }
  int mid = l + r >> 1;
  Build(x << 1, l, mid, v), Build(x << 1 | 1, mid + 1, r, v);
}

inline void ModifyMin(int x, int l, int r, int ql, int qr, int v) {
  if (l == ql && r == qr) {
    val[x] = v;
    return ;
  }
  int mid = l + r >> 1;
  if (qr <= mid) {
    ModifyMin(x << 1, l, mid, ql, qr, v);
  } else if (ql > mid) {
    ModifyMin(x << 1 | 1, mid + 1, r, ql, qr, v);
  } else {
    ModifyMin(x << 1, l, mid, ql, mid, v), ModifyMin(x << 1 | 1, mid + 1, r, mid + 1, qr, v);
  }
}

inline int QueryMin(int x, int l, int r, int p) {
  if (l == r) {
    return val[x];
  }
  int mid = l + r >> 1;
  if (p <= mid) {
    return min(val[x], QueryMin(x << 1, l, mid, p));
  } else {
    return min(val[x], QueryMin(x << 1 | 1, mid + 1, r, p));
  }
}

inline void ModifySum(int x, int l, int r, int ql, int qr) {
  if (l == ql && r == qr) {
    ++val[x];
    return ;
  }
  int mid = l + r >> 1;
  if (qr <= mid) {
    ModifySum(x << 1, l, mid, ql, qr);
  } else if (ql > mid) {
    ModifySum(x << 1 | 1, mid + 1, r, ql, qr);
  } else {
    ModifySum(x << 1, l, mid, ql, mid), ModifySum(x << 1 | 1, mid + 1, r, mid + 1, qr);
  }
}

inline int QuerySum(int x, int l, int r, int p) {
  if (l == r) {
    return val[x];
  }
  int mid = l + r >> 1;
  if (p <= mid) {
    return val[x] + QuerySum(x << 1, l, mid, p);
  } else {
    return val[x] + QuerySum(x << 1 | 1, mid + 1, r, p);
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m), ans = inf;
  for (int i = 1; i <= n; ++i) {
    Read(a[i]), Read(opt[i]);
    if (opt[i] == 1 && (a[i] << 1) > m) {
      puts("-1");
      return 0;
    }
    if (opt[i] == 2) {
      l[i] = 0, r[i] = m - 1;
    } else {
      l[i] = sum, r[i] = ((sum - 2 * a[i]) % m + m) % m;
    }
    sum = ((sum - 2 * a[i]) % m + m) % m;
    all[++tot] = l[i], all[++tot] = r[i], ans;
  }
  sort(all + 1, all + tot + 1), tot = unique(all + 1, all + tot + 1) - all - 1;
  for (int i = 1; i <= n; ++i) {
    l[i] = lower_bound(all + 1, all + tot + 1, l[i]) - all;
    r[i] = lower_bound(all + 1, all + tot + 1, r[i]) - all;
  }
  Build(1, 1, tot, n + 1);
  for (int i = n, p; i; --i) {
    p = QueryMin(1, 1, tot, l[i]);
    if (p != n + 1) {
      f[i] = f[p] + (all[l[p]] - all[l[i]] + m) % m;
    }
    p = QueryMin(1, 1, tot, r[i]);
    if (p != n + 1) {
      g[i] = f[p] + (all[l[p]] - all[r[i]] + m) % m;
    }
    if (l[i] <= r[i]) {
      if (r[i] < tot) {
        ModifyMin(1, 1, tot, r[i] + 1, tot, i);
      }
      if (l[i] > 1) {
        ModifyMin(1, 1, tot, 1, l[i] - 1, i);
      }
    } else {
      if (r[i] + 1 < l[i]) {
        ModifyMin(1, 1, tot, r[i] + 1, l[i] - 1, i);
      }
    }
  }
  Build(1, 1, tot, 0);
  for (int i = 1; i <= n; ++i) {
    if (QuerySum(1, 1, tot, l[i]) == i - 1) {
      CheckMin(ans, f[i]);
    }
    if (QuerySum(1, 1, tot, r[i]) == i - 1) {
      CheckMin(ans, g[i]);
    }
    if (l[i] <= r[i]) {
      ModifySum(1, 1, tot, l[i], r[i]);
    } else {
      ModifySum(1, 1, tot, 1, r[i]), ModifySum(1, 1, tot, l[i], tot);
    }
  }
  for (int i = 1; i <= n; ++i) {
    ans += a[i] << 1;
  }
  printf("%lld\n", ans);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}


Measured memory_bytes: 8448