Problem: There is a railroad in Takahashi Kingdom. The railroad consists of N sections, numbered 1, 2, ..., N, and N+1 stations, numbered 0, 1, ..., N. Section i directly connects the stations i-1 and i. A train takes exactly A_i minutes to run through section i, regardless of direction. Each of the N sections is either single-tracked over the whole length, or double-tracked over the whole length. If B_i = 1, section i is single-tracked; if B_i = 2, section i is double-tracked. Two trains running in opposite directions can cross each other on a double-tracked section, but not on a single-tracked section. Trains can also cross each other at a station.

Snuke is creating the timetable for this railroad. In this timetable, the trains on the railroad run every K minutes, as shown in the following figure. Here, bold lines represent the positions of trains running on the railroad. (See Sample 1 for clarification.)

<image>

When creating such a timetable, find the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. It can be proved that, if there exists a timetable satisfying the conditions in this problem, this minimum sum is always an integer.

Formally, the times at which trains arrive and depart must satisfy the following:

* Each train either departs station 0 and is bound for station N, or departs station N and is bound for station 0.
* Each train takes exactly A_i minutes to run through section i. For example, if a train bound for station N departs station i-1 at time t, the train arrives at station i exactly at time t+A_i.
* Assume that a train bound for station N arrives at a station at time s, and departs the station at time t. Then, the next train bound for station N arrives at the station at time s+K, and departs the station at time t+K. Additionally, the previous train bound for station N arrives at the station at time s-K, and departs the station at time t-K. This must also be true for trains bound for station 0.
* Trains running in opposite directions must not be running on the same single-tracked section (except the stations at both ends) at the same time.

Constraints

* 1 \leq N \leq 100000
* 1 \leq K \leq 10^9
* 1 \leq A_i \leq 10^9
* A_i is an integer.
* B_i is either 1 or 2.

Input

The input is given from Standard Input in the following format:


N K
A_1 B_1
A_2 B_2
:
A_N B_N


Output

Print an integer representing the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. If it is impossible to create a timetable satisfying the conditions, print -1 instead.

Examples

Input

3 10
4 1
3 1
4 1


Output

26


Input

1 10
10 1


Output

-1


Input

6 4
1 1
1 1
1 1
1 1
1 1
1 1


Output

12


Input

20 987654321
129662684 2
162021979 1
458437539 1
319670097 2
202863355 1
112218745 1
348732033 1
323036578 1
382398703 1
55854389 1
283445191 1
151300613 1
693338042 2
191178308 2
386707193 1
204580036 1
335134457 1
122253639 1
824646518 2
902554792 2


Output

14829091348

Solution (in CPP):

Solution:
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}

const int N = 1e5 + 10;
int n , K , a[N] , b[N] , lft[N] , rgt[N];
ll s[N] , dpL[N] , dpR[N];
vi V;

struct SegmentTree{
  int n , a[N << 4];
  int Min(int c,int l,int r,int pos) {
    if(l==r) return a[c];
    else {
      int mid=(l+r)>>1;
      return min(a[c],pos<=mid?
          Min(c<<1,l,mid,pos):Min(c<<1|1,mid+1,r,pos));
    }
  }
  void modify(int c,int l,int r,int L,int R,int x) {
    if(L<=l&&r<=R) a[c]=x;
    else {
      int mid=(l+r)>>1;
      if(L<=mid) modify(c<<1,l,mid,L,R,x);
      if(R>mid) modify(c<<1|1,mid+1,r,L,R,x);
    }
  }
  int Min(int pos) {
    return Min(1,0,n-1,pos);
  }
  void modify(int L,int R,int x) {
    modify(1,0,n-1,L,R,x);
  }
  void init(int _n) {
    n = _n;
    memset(a,0x37,sizeof(a));
  }
}sgt;

int main(){
  scanf("%d%d",&n,&K);
  ll ans = 0;
  rep(i,1,n+1) {
    scanf("%d%d",a+i,b+i);
    ans += a[i] * 2;
    s[i] = s[i-1] + a[i];
    if(b[i] == 1) {
      if(a[i] * 2 > K)
        return puts("-1") , 0;
      lft[i] = (-2 * s[i] % K + K) % K;
      rgt[i] = (-2 * s[i-1] % K + K) % K;
      V.pb(lft[i]);
      V.pb(rgt[i]);
      V.pb((lft[i] + 1) % K);
      V.pb((rgt[i] - 1 + K) % K);
    }
  }
  sort(all(V));
  V.erase(unique(all(V)),V.end());
  rep(i,1,n+1) if(b[i] == 1){
    lft[i] = lower_bound(all(V),lft[i])-V.begin();
    rgt[i] = lower_bound(all(V),rgt[i])-V.begin();
  }
  sgt.init(sz(V));
  per(i,1,n+1) if(b[i] == 1){
    int x = sgt.Min(lft[i]);
    if(x > n) dpL[i] = 0;
    else dpL[i] = dpR[x] + (V[rgt[x]] - V[lft[i]] + K) % K;
    x = sgt.Min(rgt[i]);
    if(x > n) dpR[i] = 0;
    else dpR[i] = dpR[x] + (V[rgt[x]] - V[rgt[i]] + K) % K;
    int l = (lft[i] + 1) % sz(V);
    int r = (rgt[i] - 1 + sz(V)) % sz(V);
    if(l <= r) sgt.modify(l , r , i);
    else sgt.modify(0 , r , i) , sgt.modify(l , sz(V) - 1 , i);
  }
  ll ans2 = *min_element(b+1,b+1+n)==2?0:1ll<<62;
  sgt.init(sz(V));
  rep(i,1,n+1) if(b[i] == 1){
    int x = sgt.Min(lft[i]);
    if(x > n) ans2 = min(ans2 , dpL[i]);
    x = sgt.Min(rgt[i]);
    if(x > n) ans2 = min(ans2 , dpR[i]);
    int l = (lft[i] + 1) % sz(V);
    int r = (rgt[i] - 1 + sz(V)) % sz(V);
    if(l <= r) sgt.modify(l , r , 0);
    else sgt.modify(0 , r , 0) , sgt.modify(l , sz(V) - 1 , 0);
  }
  printf("%lld\n",ans + ans2);
  return 0;
}