#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
using namespace std;

typedef long long LL;
typedef pair < int, int > pa;

const int MAXN = 100010;
const LL INF = 1e18;

int n, K, A[MAXN], L[MAXN], R[MAXN];
int ls[MAXN << 1], tot, type[MAXN], e[MAXN << 2], sum;
LL f[MAXN], g[MAXN], ans = INF;

inline void modify(int x, int l, int r, int ql, int qr, int v)
{
	if( l == ql && r == qr ) { e[ x ] = v; return ; }
	int mid = l + r >> 1;
	if( qr <= mid ) modify( x << 1, l, mid, ql, qr, v );
	else if( ql > mid ) modify( x << 1 | 1, mid + 1, r, ql, qr, v );
	else modify( x << 1, l, mid, ql, mid, v ), modify( x << 1 | 1, mid + 1, r, mid + 1, qr, v );
}

inline int querymin(int x, int l, int r, int v)
{
	if( l == r ) return e[ x ];
	int mid = l + r >> 1;
	if( v <= mid ) return min( e[ x ], querymin( x << 1, l, mid, v ) );
	return min( e[ x ], querymin( x << 1 | 1, mid + 1, r, v ) );
}

inline void add(int x, int l, int r, int ql, int qr)
{
	if( l == ql && r == qr ) { e[ x ]++; return ; }
	int mid = l + r >> 1;
	if( qr <= mid ) add( x << 1, l, mid, ql, qr );
	else if( ql > mid ) add( x << 1 | 1, mid + 1, r, ql, qr );
	else add( x << 1, l, mid, ql, mid ), add( x << 1 | 1, mid + 1, r, mid + 1, qr );
}

inline int querysum(int x, int l, int r, int v)
{
	if( l == r ) return e[ x ];
	int mid = l + r >> 1;
	if( v <= mid ) return e[ x ] + querysum( x << 1, l, mid, v );
	return e[ x ] + querysum( x << 1 | 1, mid + 1, r, v );
}

int main()
{
#ifdef wxh010910
	freopen( "data.in", "r", stdin );
	freopen( "my.out", "w", stdout );
#endif
	scanf( "%d%d", &n, &K );
	for( int i = 1 ; i <= n ; i++ )
	{
		scanf( "%d%d", &A[ i ], &type[ i ] );
		if( A[ i ] * 2 > K && type[ i ] == 1 ) return printf( "-1\n" ), 0;
		if( type[ i ] == 2 ) L[ i ] = 0, R[ i ] = K - 1;
		else L[ i ] = sum, R[ i ] = ( ( L[ i ] - 2 * A[ i ] ) % K + K ) % K;
		sum = ( ( sum - 2 * A[ i ] ) % K + K ) % K;
	}
	for( int i = 1 ; i <= n ; i++ ) ls[ ++tot ] = L[ i ], ls[ ++tot ] = R[ i ];
	sort( ls + 1, ls + tot + 1 ); tot = unique( ls + 1, ls + tot + 1 ) - ls - 1;
	for( int i = 1 ; i <= n ; i++ ) L[ i ] = lower_bound( ls + 1, ls + tot + 1, L[ i ] ) - ls, R[ i ] = lower_bound( ls + 1, ls + tot + 1, R[ i ] ) - ls;
	for( int i = 1 ; i <= ( n << 2 ) ; i++ ) e[ i ] = n + 1;
	for( int i = n, pos ; i ; i-- )
	{
		pos = querymin( 1, 1, tot, L[ i ] );
		if( pos != n + 1 ) f[ i ] = f[ pos ] + ( ls[ L[ pos ] ] - ls[ L[ i ] ] + K ) % K;
		pos = querymin( 1, 1, tot, R[ i ] );
		if( pos != n + 1 ) g[ i ] = f[ pos ] + ( ls[ L[ pos ] ] - ls[ R[ i ] ] + K ) % K;
		if( L[ i ] <= R[ i ] )
		{
			if( R[ i ] < tot ) modify( 1, 1, tot, R[ i ] + 1, tot, i );
			if( L[ i ] > 1 ) modify( 1, 1, tot, 1, L[ i ] - 1, i );
		}
		else if( R[ i ] + 1 < L[ i ] ) modify( 1, 1, tot, R[ i ] + 1, L[ i ] - 1, i );
	}
	for( int i = 1 ; i <= ( n << 2 ) ; i++ ) e[ i ] = 0;
	for( int i = 1 ; i <= n ; i++ )
	{
		if( querysum( 1, 1, tot, L[ i ] ) == i - 1 )
			ans = min( ans, f[ i ] );
		if( querysum( 1, 1, tot, R[ i ] ) == i - 1 )
			ans = min( ans, g[ i ] );
		if( L[ i ] <= R[ i ] ) add( 1, 1, tot, L[ i ], R[ i ] );
		else add( 1, 1, tot, L[ i ], tot ), add( 1, 1, tot, 1, R[ i ] );
	}
	for( int i = 1 ; i <= n ; i++ ) ans += A[ i ] << 1;
	cout << ans << endl;
}


Measured memory_bytes: 5760