#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

typedef long long s64;

inline int getint()
{
	static char c;
	while ((c = getchar()) < '0' || c > '9');

	int res = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		res = res * 10 + c - '0';
	return res;
}

const int MaxN = 100000;
const int MaxM = MaxN * 2;
const int MaxSegN = MaxM * 4;

int n, m, k;
int vl[MaxN + 1], vr[MaxN + 1];

int li[MaxM + 1];

int seg[MaxSegN];

inline void seg_tag_down(int p)
{
	if (seg[p] > 0)
	{
		seg[p << 1 | 0] = seg[p];
		seg[p << 1 | 1] = seg[p];
		seg[p] = 0;
	}
}

void seg_cover(int p, int pL, int pR, int qL, int qR, int v)
{
	if (qL > qR)
		return;
	if (qL <= pL && qR >= pR)
	{
		seg[p] = v;
		return;
	}

	seg_tag_down(p);

	int pM = pL + pR >> 1;
	if (qL <= pM)
		seg_cover(p << 1 | 0, pL, pM, qL, qR, v);
	if (qR > pM)
		seg_cover(p << 1 | 1, pM + 1, pR, qL, qR, v);
}

int seg_query(int p, int pL, int pR, int q)
{
	if (pL == pR)
		return seg[p];

	seg_tag_down(p);

	int pM = pL + pR >> 1;
	if (q <= pM)
		return seg_query(p << 1 | 0, pL, pM, q);
	if (q > pM)
		return seg_query(p << 1 | 1, pM + 1, pR, q);
}

s64 f[MaxN + 1];

inline s64 query(int l)
{
	int v = seg_query(1, 1, m, l);
	return !v ? 0 : f[v] + (li[vl[v]] - li[l] + k) % k;
}

int main()
{
	cin >> n >> k;

	s64 sum = 0;
	int pre = 0;
	for (int i = 1; i <= n; ++i)
	{
		int a = getint() * 2;
		int t = getint();
		sum += a;

		if (t == 1 && a > k)
		{
			cout << -1 << endl;
			return 0;
		}

		a = (pre - a % k + k) % k;

		if (t == 2)
			vl[i] = 0, vr[i] = k - 1;
		else
			vl[i] = pre, vr[i] = a;

		pre = a;

		li[++m] = vl[i];
		li[++m] = vr[i];
	}

	sort(li + 1, li + m + 1);
	m = unique(li + 1, li + m + 1) - li - 1;

	for (int i = n; i >= 1; --i)
	{
		vl[i] = lower_bound(li + 1, li + m + 1, vl[i]) - li;
		vr[i] = lower_bound(li + 1, li + m + 1, vr[i]) - li;
		f[i] = query(vl[i]);

		if (vl[i] <= vr[i])
		{
			seg_cover(1, 1, m, 1, vl[i] - 1, i);
			seg_cover(1, 1, m, vr[i] + 1, m, i);
		}
		else
			seg_cover(1, 1, m, vr[i] + 1, vl[i] - 1, i);
	}

	s64 res = f[1];
	for (int l = 1; l <= m; ++l)
		res = min(res, query(l));

	cout << res + sum << endl;

	return 0;
}