#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(v) v.begin(),v.end()

const int maxn = 1e5 + 100;
const ll inf = 2e18, mod = 1e9 + 7;

int n, b[maxn], seg[4 * maxn];
ll k, a[maxn], l[maxn], r[maxn], dp[maxn];
vector<ll> compress;

void update(int u, int l, int r, int b, int e, int val) { 
	if (b >= compress.size() || b >= e) {
		return;
	}
	
	if (b <= l && r <= e) {
		seg[u] = min(seg[u], val);
		return;
	}

	int mid = (l + r) / 2;

	if (b < mid) {
		update(2 * u, l, mid, b, e, val);
	}
	if (e > mid) {
		update(2 * u + 1, mid, r, b, e, val);
	}
}

int query(int u, int l, int r, int x) {
	if (r - l == 1) {
		return seg[u];
	}

	int mid = (l + r) / 2;
	if (x < mid) {
		return min(seg[u], query(2 * u, l, mid, x));
	}
	else {
		return min(seg[u], query(2 * u + 1, mid, r, x));
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	fill(seg, seg + 4 * maxn, mod);

	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
		if (b[i] == 1 && 2 * a[i] > k) {
			cout << "-1" << endl;
			return 0;
		}

		if (i) {
			a[i] += a[i - 1];
		}
		if (b[i] == 1) {
			l[i] = (-2 * (i ? a[i - 1] : 0) % k + k) % k;
			r[i] = (-2 * a[i] % k + k) % k;
		}
		else {
			l[i] = 0;
			r[i] = k - 1;
		}
		compress.pb(l[i]);
		compress.pb(r[i]);
	}
	sort(all(compress));
	compress.resize(unique(all(compress)) - compress.begin());

	for (int i = 0; i < n; i++) {
		l[i] = lower_bound(all(compress), l[i]) - compress.begin();
		r[i] = lower_bound(all(compress), r[i]) - compress.begin();
	}

	for (int i = n - 1; i >= 0; i--) {
		int pos = query(1, 0, compress.size(), l[i]);
		if (pos < n) {
			dp[i] = dp[pos] + (compress[l[pos]] - compress[l[i]] + k) % k;
		}
		else {
			dp[i] = 0;
		}

		if (l[i] <= r[i]) {
			update(1, 0, compress.size(), r[i] + 1, compress.size(), i);
			update(1, 0, compress.size(), 0, l[i], i);
		}
		else {
			update(1, 0, compress.size(), r[i] + 1, l[i], i);
		}
	}

	ll ans = inf;
	for (int i = 0; i < compress.size(); i++) {
		int val = query(1, 0, compress.size(), i);
		if (val >= n) {
			ans = 0;
		}
		else {
			ans = min(ans, dp[val] + (compress[l[val]] - compress[i] + k) % k);
		}
	}

	cout << ans + (2 * a[n - 1]) << endl;
}

