#include"bits/stdc++.h"


#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(int x = (l);x<=(u);x++)
#define RREP(x,l,u) for(int x = (l);x>=(u);x--)
#define mst(x,a) memset(x,a,sizeof(x))
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define se second
#define fi first
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define cl(x) x.clear()

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;

const int maxn = 200010;
const int mod = 1e9+7;
const int MAX = 1000000010;
const double eps = 1e-6;
const double PI = acos(-1);

template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);}
template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);}
template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);}
template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;}
template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;}

int n,K;

ll A[maxn],B[maxn],L[maxn],R[maxn];

int hs[maxn],cnt;

ll f[maxn],g[maxn],ans=1e18;

struct Node{
	int l,r,mn;
}T[maxn<<2];

void build(int i,int l,int r){
	T[i].l=l;T[i].r=r;T[i].mn=n+1;
	if(l==r)return;
	int M=l+r>>1;
	build(i<<1,l,M);build(i<<1|1,M+1,r);
}

int query(int i,int x){
	if(T[i].l==T[i].r)return T[i].mn;
	int ans=T[i].mn,M=T[i].l+T[i].r>>1;
	if(x<=M)ans=min(ans,query(i<<1,x));else ans=min(ans,query(i<<1|1,x));
	return ans;
}

void update(int i,int l,int r,int x){
	if(l>r)return;
	if(l<=T[i].l&&T[i].r<=r){
		T[i].mn=min(T[i].mn,x);
		return;
	}
	int M=T[i].l+T[i].r>>1;
	if(l<=M)update(i<<1,l,r,x);if(r>M)update(i<<1|1,l,r,x);
}

int c[maxn];

void add(int x,int y){
	for(;x<=cnt;x+=x&-x)c[x]+=y;
}
int query(int x){
	int ans=0;
	for(;x;x-=x&-x)ans+=c[x];
	return ans;
}

void Work(){
	int now=0;
	REP(i,1,n){
		if(B[i]==2)L[i]=0,R[i]=K-1;
		else{
			if(A[i]*2>K){
				puts("-1");
				return;
			}
			L[i]=now;R[i]=(now-A[i]*2+K)%K;
		}
		now=(now-(A[i]*2)%K+K)%K;
	}
	REP(i,1,n)hs[++cnt]=L[i],hs[++cnt]=R[i];
	sort(hs+1,hs+cnt+1);cnt=unique(hs+1,hs+cnt+1)-hs-1;
	REP(i,1,n)L[i]=LB(hs+1,hs+cnt+1,L[i])-hs,R[i]=LB(hs+1,hs+cnt+1,R[i])-hs;
	build(1,1,cnt);
	RREP(i,n,1){
		int t;
		t=query(1,L[i]);
		if(t!=n+1)f[i]=min(f[t]+(hs[L[t]]-hs[L[i]]+K)%K,g[t]+(hs[R[t]]-hs[L[i]]+K)%K);
		t=query(1,R[i]);
		if(t!=n+1)g[i]=min(f[t]+(hs[L[t]]-hs[R[i]]+K)%K,g[t]+(hs[R[t]]-hs[R[i]]+K)%K);
		if(L[i]<=R[i])update(1,1,L[i]-1,i),update(1,R[i]+1,cnt,i);
		else update(1,R[i]+1,L[i]-1,i);
	}
	REP(i,1,n){
		if(query(L[i])==i-1)ans=min(ans,f[i]);
		if(query(R[i])==i-1)ans=min(ans,g[i]);
		if(L[i]<=R[i])add(L[i],1),add(R[i]+1,-1);
		else add(1,1),add(R[i]+1,-1),add(L[i],1);
	}
	REP(i,1,n)ans+=A[i]*2;
	cout<<ans<<endl;
}

void Init(){
	read(n,K);
	REP(i,1,n)read(A[i],B[i]);
}

int main(){
	Init();
	Work();
	return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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

### Response

```cpp
#include"bits/stdc++.h"


#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(int x = (l);x<=(u);x++)
#define RREP(x,l,u) for(int x = (l);x>=(u);x--)
#define mst(x,a) memset(x,a,sizeof(x))
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define se second
#define fi first
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define cl(x) x.clear()

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;

const int maxn = 200010;
const int mod = 1e9+7;
const int MAX = 1000000010;
const double eps = 1e-6;
const double PI = acos(-1);

template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);}
template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);}
template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);}
template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;}
template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;}

int n,K;

ll A[maxn],B[maxn],L[maxn],R[maxn];

int hs[maxn],cnt;

ll f[maxn],g[maxn],ans=1e18;

struct Node{
	int l,r,mn;
}T[maxn<<2];

void build(int i,int l,int r){
	T[i].l=l;T[i].r=r;T[i].mn=n+1;
	if(l==r)return;
	int M=l+r>>1;
	build(i<<1,l,M);build(i<<1|1,M+1,r);
}

int query(int i,int x){
	if(T[i].l==T[i].r)return T[i].mn;
	int ans=T[i].mn,M=T[i].l+T[i].r>>1;
	if(x<=M)ans=min(ans,query(i<<1,x));else ans=min(ans,query(i<<1|1,x));
	return ans;
}

void update(int i,int l,int r,int x){
	if(l>r)return;
	if(l<=T[i].l&&T[i].r<=r){
		T[i].mn=min(T[i].mn,x);
		return;
	}
	int M=T[i].l+T[i].r>>1;
	if(l<=M)update(i<<1,l,r,x);if(r>M)update(i<<1|1,l,r,x);
}

int c[maxn];

void add(int x,int y){
	for(;x<=cnt;x+=x&-x)c[x]+=y;
}
int query(int x){
	int ans=0;
	for(;x;x-=x&-x)ans+=c[x];
	return ans;
}

void Work(){
	int now=0;
	REP(i,1,n){
		if(B[i]==2)L[i]=0,R[i]=K-1;
		else{
			if(A[i]*2>K){
				puts("-1");
				return;
			}
			L[i]=now;R[i]=(now-A[i]*2+K)%K;
		}
		now=(now-(A[i]*2)%K+K)%K;
	}
	REP(i,1,n)hs[++cnt]=L[i],hs[++cnt]=R[i];
	sort(hs+1,hs+cnt+1);cnt=unique(hs+1,hs+cnt+1)-hs-1;
	REP(i,1,n)L[i]=LB(hs+1,hs+cnt+1,L[i])-hs,R[i]=LB(hs+1,hs+cnt+1,R[i])-hs;
	build(1,1,cnt);
	RREP(i,n,1){
		int t;
		t=query(1,L[i]);
		if(t!=n+1)f[i]=min(f[t]+(hs[L[t]]-hs[L[i]]+K)%K,g[t]+(hs[R[t]]-hs[L[i]]+K)%K);
		t=query(1,R[i]);
		if(t!=n+1)g[i]=min(f[t]+(hs[L[t]]-hs[R[i]]+K)%K,g[t]+(hs[R[t]]-hs[R[i]]+K)%K);
		if(L[i]<=R[i])update(1,1,L[i]-1,i),update(1,R[i]+1,cnt,i);
		else update(1,R[i]+1,L[i]-1,i);
	}
	REP(i,1,n){
		if(query(L[i])==i-1)ans=min(ans,f[i]);
		if(query(R[i])==i-1)ans=min(ans,g[i]);
		if(L[i]<=R[i])add(L[i],1),add(R[i]+1,-1);
		else add(1,1),add(R[i]+1,-1),add(L[i],1);
	}
	REP(i,1,n)ans+=A[i]*2;
	cout<<ans<<endl;
}

void Init(){
	read(n,K);
	REP(i,1,n)read(A[i],B[i]);
}

int main(){
	Init();
	Work();
	return 0;
}

```