#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int oo = 0x3f3f3f3f;

const int maxn = 100100;

int n, K;

int a[maxn + 5], b[maxn + 5];

int st[maxn + 5];

pair<int, int> range[maxn + 5];

LL ans = 0;
 
struct node
{
	node *c[2];

	int label;
 
	node(): label(-1) { memset(c, 0, sizeof c); }
 
	void flag_label(int _label)
	{
		if (~_label) chkmax(label, _label);
	}

	void push_down()
	{
		if (~label)
		{
			REP(i, 0, 2) c[i]->flag_label(label);
			label = -1;
		}
	}
 
};

const int max0 = 100000;
 
node *nd_pool;
int nd_res = 0;
 
inline node *newnode()
{
	if (!nd_res) nd_pool = new node[max0], nd_res = max0;
	return nd_pool + (--nd_res);
}
 
int seg_x, seg_y, seg_z, seg_ret;
 
void add(node *&rt, int l, int r)
{
	if (!rt) rt = newnode();
	if (seg_x <= l && r <= seg_y)
	{
		rt->flag_label(seg_z);
		return;
	}
	int mid = (l + r) >> 1;
	if (seg_x < mid) add(rt->c[0], l, mid);
	if (seg_y > mid) add(rt->c[1], mid, r);
}
 
void query(node *rt, int l, int r)
{
	if (!rt) return;
	if (~rt->label) chkmax(seg_ret, rt->label);
	if (r - l <= 1) return;
	int mid = (l + r) >> 1;
	if (seg_x < mid) query(rt->c[0], l, mid);
	else query(rt->c[1], mid, r);
}
 
node *rt;

inline void seg_add(int x, int y, int z)
{
	seg_x = x, seg_y = y, seg_z = z;
	add(rt, 0, K);
}

inline int seg_query(int x)
{
	seg_x = x, seg_ret = -1;
	query(rt, 0, K);
	return seg_ret;
}

LL dp[maxn + 5];

int main()
{
#ifdef matthew99
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &K);
	LL sum = 0;
	REP(i, 0, n) scanf("%d%d", a + i, b + i), sum += a[i] * 2;
	REP(i, 0, n) if (b[i] == 1 && a[i] * 2 > K)
	{
		puts("-1");
		return 0;
	}
	st[0] = 0;
	REP(i, 0, n) (st[i + 1] = st[i] + a[i]) %= K;
	ans = LLONG_MAX;
	int pre = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		(pre += a[i]) %= K;
		(st[i] += K - pre) %= K;
	}
	rt = NULL;
	REP(i, 0, n)
	{
		int tmp = seg_query(st[i]);
		if (~tmp) assert(tmp < i - 1), dp[i] = dp[tmp + 1] + (st[tmp + 1] - st[i] + K) % K;
		else dp[i] = 0;
		if (b[i] == 1)
		{
			int l = st[i + 1] - (a[i] << 1) + 1, r = st[i + 1];
			int l0 = max(0, l), r0 = min(K, r);
			int l1 = max(0, l + K), r1 = min(K, r + K);
			if (l0 < r0)
			{
				seg_add(l0, r0, i);
			}
			if (l1 < r1)
			{
				seg_add(l1, r1, i);
			}
		}
	}
	rt = NULL;
	for (int i = n - 1; i >= 0; --i)
	{
		if (b[i] == 1)
		{
			int l = st[i + 1] - (a[i] << 1) + 1, r = st[i + 1];
			int l0 = max(0, l), r0 = min(K, r);
			int l1 = max(0, l + K), r1 = min(K, r + K);
			if (l0 < r0)
			{
				seg_add(l0, r0, 0);
			}
			if (l1 < r1)
			{
				seg_add(l1, r1, 0);
			}
		}
		if (!~seg_query(st[i])) 
		{
			chkmin(ans, dp[i]);
		}
	}
	ans += sum;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 124032