#include<stdio.h>

int main(){
    int small, large, wsmall, n ,m;
    int arr[101] = {0}, brr[101] = {0}, i, j;
    scanf("%d %d", &n, &m);
    for(i=0; i<n; i++){
        scanf("%d", &j);
        arr[j]++;
    }
    for(i=0; i<m; i++){
        scanf("%d", &j);
        brr[j]++;
    }
    for(i=0; i<101; i++){
        if(arr[i]){
            small = i;
            break;
        }
    }
    for(i=100; i>=0; i--){
        if(arr[i]){
            large = i;
            break;
        }
    }
    for(i=0; i<101; i++){
        if(brr[i]){
            wsmall = i;
            break;
        }
    }
    if((2*small) > large && large != small)
        printf("-1");
    else if(large > wsmall || (2 * small) >= wsmall){
        printf("-1");
    }
    else{
        if(large > (2 * small)){
            printf("%d", large);
        }
        else{
            printf("%d", (2 * small));
        }
    }
return 0;
}


#include<stdio.h>

int main(){
    int small, large, wsmall, n ,m;
    int arr[101] = {0}, brr[102] = {0}, i, j;
    scanf("%d %d", &n, &m);
    for(i=0; i<n; i++){
        scanf("%d", &j);
        arr[j]++;
    }
    for(i=0; i<m; i++){
        scanf("%d", &j);
        brr[j]++;
    }
    for(i=0; i<101; i++){
        if(arr[i]){
            small = i;
            break;
        }
    }
    for(i=100; i>=0; i--){
        if(arr[i]){
            large = i;
            break;
        }
    }
    for(i=0; i<101; i++){
        if(brr[i]){
            wsmall = i;
            break;
        }
    }
   if(large >= wsmall || (2 * small) >= wsmall){
        printf("-1");
    }
    else{
        if(large > (2 * small)){
            printf("%d", large);
        }
        else{
            printf("%d", (2 * small));
        }
    }
return 0;
}


#include<stdio.h>

int main(){
    int small, large, wsmall, n ,m;
    int arr[101] = {0}, brr[101] = {0}, i, j;
    scanf("%d %d", &n, &m);
    for(i=0; i<n; i++){
        scanf("%d", &j);
        arr[j]++;
    }
    for(i=0; i<m; i++){
        scanf("%d", &j);
        brr[j]++;
    }
    for(i=0; i<101; i++){
        if(arr[i]){
            small = i;
            break;
        }
    }
    for(i=100; i>=0; i--){
        if(arr[i]){
            large = i;
            break;
        }
    }
    for(i=0; i<101; i++){
        if(brr[i]){
            wsmall = i;
            break;
        }
    }
   if(large >= wsmall || (2 * small) >= wsmall){
        printf("-1");
    }
    else{
        if(large > (2 * small)){
            printf("%d", large);
        }
        else{
            printf("%d", (2 * small));
        }
    }
return 0;
}


#include<stdio.h>

int main(){
    int small, large, wsmall, n ,m;
    int arr[101] = {0}, brr[101] = {0}, i, j;
    scanf("%d %d", &n, &m);
    for(i=0; i<n; i++){
        scanf("%d", &j);
        arr[j]++;
    }
    for(i=0; i<m; i++){
        scanf("%d", &j);
        brr[j]++;
    }
    for(i=0; i<101; i++){
        if(arr[i]){
            small = i;
            break;
        }
    }
    for(i=100; i>=0; i--){
        if(arr[i]){
            large = i;
            break;
        }
    }
    for(i=0; i<101; i++){
        if(brr[i]){
            wsmall = i;
            break;
        }
    }
    if((2*small) > large)
        printf("-1");
    else if(large > wsmall){
        printf("-1");
    }
    else{
        if(large > (2 * small)){
            printf("%d", large);
        }
        else{
            printf("%d", (2 * small));
        }
    }
return 0;
}


#include<stdio.h>

int main(){
    int small, large, wsmall, n ,m;
    int arr[101] = {0}, brr[101] = {0}, i, j;
    scanf("%d %d", &n, &m);
    for(i=0; i<n; i++){
        scanf("%d", &j);
        arr[j]++;
    }
    for(i=0; i<m; i++){
        scanf("%d", &j);
        brr[j]++;
    }
    for(i=0; i<101; i++){
        if(arr[i]){
            small = i;
            break;
        }
    }
    for(i=100; i>=0; i--){
        if(arr[i]){
            large = i;
            break;
        }
    }
    for(i=0; i<101; i++){
        if(brr[i]){
            wsmall = i;
            break;
        }
    }
    if((2*small) > large && large != small)
        printf("-1");
    else if(large > wsmall){
        printf("-1");
    }
    else{
        if(large > (2 * small)){
            printf("%d", large);
        }
        else{
            printf("%d", (2 * small));
        }
    }
return 0;
}


/*
 *********************************
 *Description:
 *Author: ruoshui
 *Date: 10/02/13
 *********************************
 */

#include <stdio.h>
#include <stdlib.h>

#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define MIN( x, y ) ( ( x ) > ( y ) ? ( y ) : ( x ) )
typedef long long ll;
int n, m;
int smax = 0, smin = 128, fm = 128;
int main( )
{
	int i, temp;
	for ( scanf( "%d %d", &n, &m )  , i = 0; i < n; ++i  ){
		scanf( "%d", &temp );
		smax = MAX( smax, temp );
		smin = MIN( smin, temp );
	}
	for ( i = 0; i < m; ++i ){
		scanf( "%d", &temp );
		fm = MIN( fm, temp );
	}
	if( smax > 2 * smin && fm > smax ) {
		printf( "%d\n", smax );
	} else {
		printf( "-1\n" );
	}
	return 0;
}


/*
 *********************************
 *Description:
 *Author: ruoshui
 *Date: 10/02/13
 *********************************
 */

#include <stdio.h>
#include <stdlib.h>

#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define MIN( x, y ) ( ( x ) > ( y ) ? ( y ) : ( x ) )
typedef long long ll;
int n, m;
int smax = 0, smin = 128, fm = 128;
int main( )
{
	int i, temp;
	for ( scanf( "%d %d", &n, &m )  , i = 0; i < n; ++i  ){
		scanf( "%d", &temp );
		smax = MAX( smax, temp );
		smin = MIN( smin, temp );
	}
	for ( i = 0; i < m; ++i ){
		scanf( "%d", &temp );
		fm = MIN( fm, temp );
	}
	if( smax >= 2 * smin && fm > smax ) {
		printf( "%d\n", smax );
	} else if ( smax == smin && fm > smax * 2 )  {
		printf( "%d\n", smax * 2 );
	} else {
		printf( "-1\n" );
	}
	return 0;
}


/*
 *********************************
 *Description:
 *Author: ruoshui
 *Date: 10/02/13
 *********************************
 */

#include <stdio.h>
#include <stdlib.h>

#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define MIN( x, y ) ( ( x ) > ( y ) ? ( y ) : ( x ) )
typedef long long ll;
int n, m;
int smax = 0, smin = 128, fm = 128;
int main( )
{
	int i, temp, max;
	for ( scanf( "%d %d", &n, &m )  , i = 0; i < n; ++i  ){
		scanf( "%d", &temp );
		smax = MAX( smax, temp );
		smin = MIN( smin, temp );
	}
	for ( i = 0; i < m; ++i ){
		scanf( "%d", &temp );
		fm = MIN( fm, temp );
	}
	max = MAX( smax, smin * 2 );
	if ( max < fm ){
		printf( "%d\n",  max );
	} else {
		printf( "-1\n" );
	}
	return 0;
}


#include<stdio.h>
int main()
{     int n,m,tl,a,b,min1,max,min2,p=0,q=0;
      scanf("%d%d",&n,&m);
      while(n--)
      { scanf("%d",&a);
        if(p==0) { min1=a;max=a; p=1; }
        else { if(min1>a) min1=a; if(max<a) max=a; }
      }
      while(m--)
      { scanf("%d",&b);
        if(q==0) { min2=b; q=1; }
        else if(b<min2) min2=b;
      }
      if((2*min1)<min2)
      { if(max>(2*min1)) printf("%d",max);
        else printf("%d",2*min1);
      }
      else printf("-1");
      return 0;
}


#include<stdio.h>
int main()
{     int n,m,tl,a,b,min1,max,min2,p=0,q=0;
      scanf("%d%d",&n,&m);
      while(n--)
      { scanf("%d",&a);
        if(p==0) { min1=a;max=a; p=1; }
        else { if(min1>a) min1=a; if(max<a) max=a; }
      }
      while(m--)
      { scanf("%d",&b);
        if(q==0) { min2=b; q=1; }
        else if(b<min2) min2=b;
      }
      if((2*min1)<min2 && max<min2)
      { if(max>(2*min1)) printf("%d",max);
        else printf("%d",2*min1);
      }
      else printf("-1");
      return 0;
}


#include<stdio.h>
int main()
{     int n,m,tl,a,b,min1,max,min2,p=0,q=0;
      scanf("%d%d",&n,&m);
      while(n--)
      { scanf("%d",&a);
        if(p==0) { min1=a;max=a; p=1; }
        else { if(min1>a) min1=a; if(max<a) max=a; }
      }
      while(m--)
      { scanf("%d",&b);
        if(q==0) { min2=b; q=1; }
        else if(b<min2) min2=b;
      }
      if((2*min1)<min2 && max>min2)
      { if(max>(2*min1)) printf("%d",max);
        else printf("%d",2*min1);
      }
      else printf("-1");
      return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mib)
            mib=b[i];
    }
    if(n==1)
        {

       if((a[0]*2)<mib )
    {
        printf("%d",(a[0]*2));
        return 0;
    }
    else
       {
        printf("-1");
        return 0;}
    }
    if(mxa>=mib)
    {
        printf("-1");
        return 0;
    }
    if((2*mia)>mxa && (2*mia)<mib)
    {
        printf("%d",(2*mia));
        return 0;
    }
    else if((2*mia)<=mxa)
       {
        printf("%d",mxa);
        return 0;
    }
    else
      {
        printf("-1");
        return 0;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mib)
            mib=b[i];
    }
    z=mxa;ck=0;
   // printf("mxa %d mia %d mib %d z %d\n",mxa,mia,mib,z);
    /*while(z<mib)
    {
        if((2*mia)<=z)
        {ck=1;break;}
        else
            z++;
    }*/
    if(n==1)
        {

       if((a[0]*2)<mib )
    {
        printf("%d",(a[0]*2));
        return 0;
    }
    else
       {
        printf("-1");
        return 0;}
    }
    if(mxa>=mib)
    {
        printf("-1");
        return 0;
    }
    if((2*mia)<mib)
    {
        printf("%d",(2*mia));
        return 0;
    }
    else
      {
        printf("-1");
        return 0;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mib)
            mib=b[i];
    }
    z=mxa;ck=0;
   // printf("mxa %d mia %d mib %d z %d\n",mxa,mia,mib,z);
    /*while(z<mib)
    {
        if((2*mia)<=z)
        {ck=1;break;}
        else
            z++;
    }*/

    if(mxa>=mib)
    {
        printf("-1");
        return 0;
    }
    if((2*mia)<=mxa)
    {
        printf("%d",mxa);
        return 0;
    }
    else
      {
        printf("-1");
        return 0;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mib)
            mib=b[i];
    }
    z=mxa;ck=0;
   // printf("mxa %d mia %d mib %d z %d\n",mxa,mia,mib,z);
    /*while(z<mib)
    {
        if((2*mia)<=z)
        {ck=1;break;}
        else
            z++;
    }*/
    if(n==1)
        {

       if((a[0]*2)<mib )
    {
        printf("%d",(a[0]*2));
        return 0;
    }
    else
       {
        printf("-1");
        return 0;}
    }
    if(mxa>=mib)
    {
        printf("-1");
        return 0;
    }
    if((2*mia)<mib)
    {
        printf("%d",mxa);
        return 0;
    }
    else
      {
        printf("-1");
        return 0;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mib)
            mib=b[i];
    }
    z=mxa;ck=0;
   // printf("mxa %d mia %d mib %d z %d\n",mxa,mia,mib,z);
    /*while(z<mib)
    {
        if((2*mia)<=z)
        {ck=1;break;}
        else
            z++;
    }*/
    if(n==1)
        {

       if((a[0]*2)<mib )
    {
        printf("%d",(a[0]*2));
        return 0;
    }
    else
       {
        printf("-1");
        return 0;}
    }
    if(mxa>=mib)
    {
        printf("-1");
        return 0;
    }
    if((2*mia)<=mxa)
    {
        printf("%d",mxa);
        return 0;
    }
    else
      {
        printf("-1");
        return 0;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,ck,z,mia,mib,mxa,i;
    scanf("%d %d",&n,&m);
    int a[n],b[m];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&b[i]);
    }
    mxa=a[0];mia=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<mia)
            mia=a[i];
        else if(a[i]>mxa)
            mxa=a[i];
    }
    mib=b[0];
    for(i=1;i<m;i++)
    {
        if(b[i]<mia)
            mib=b[i];
    }
    z=mxa;ck=0;
   // printf("mxa %d mia %d mib %d z %d\n",mxa,mia,mib,z);
    while(z<mib)
    {
        if((2*mia)<=z)
        {ck=1;break;}
        else
            z++;
    }
    if(ck==0)
        printf("-1");
    else
        printf("%d",z);

    return 0;
}


#include <stdio.h>

int main ()
{
    int n ,m ;
    int a[100];
    int i;
    int b[100];
    scanf("%d%d",&n,&m);
    for(i =0 ;i <n;i++){
            scanf("%d",&a[i]);
    }
    for(i =0 ;i <m ;i++){
        scanf("%d",&b[i]);
    }

  int max = a[0];

  int min1 = a[0];

  for (i = 0; i < n; i++)

   {

      if (a[i] > max)

        {

          max = a[i];

        }

      else if (a[i] < min1)

        {

          min1 = a[i];

        }

    }

        int min = b[0];
        for(i = 0 ;i < m ; i++){
            if(b[i] < min){
                min = b[i] ;
            }
        }

        if(max > min){
            printf("-1");
            system("pause");
            return 0;
        }
        else if((2*min1 < min) && (2*min1 > max)){
            printf("%d",2*min1);
            system("pause");
            return 0;
        }
        else if ( 2*min1 < max){
                printf("%d",max);
            system("pause");
            return 0;

        }
        else {
            printf("-1");
            system("pause");
            return 0;
        }

}


#include <stdio.h>

int main ()
{
    int n ,m ;
    int a[100];
    int i;
    int b[100];
    scanf("%d%d",&n,&m);
    for(i =0 ;i <n;i++){
            scanf("%d",&a[i]);
    }
    for(i =0 ;i <m ;i++){
        scanf("%d",&b[i]);
    }

  int max = a[0];

  int min1 = a[0];

  for (i = 0; i < n; i++)

   {

      if (a[i] > max)

        {

          max = a[i];

        }

      else if (a[i] < min1)

        {

          min1 = a[i];

        }

    }

        int min = b[0];
        for(i = 0 ;i < m ; i++){
            if(b[i] < min){
                min = b[i] ;
            }
        }

        if(max > min){
            printf("-1");

        }
        else if((2*min1 < min) && (2*min1 > max)){
            printf("%d",2*min1);

        }
        else if ( 2*min1 <= max){
                printf("%d",max);

    ;

        }
        else {
            printf("-1");

        }
        return 0;

}


#include <stdio.h>

int main ()
{
    int n ,m ;
    int a[100];
    int i;
    int b[100];
    scanf("%d%d",&n,&m);
    for(i =0 ;i <n;i++){
            scanf("%d",&a[i]);
    }
    for(i =0 ;i <m ;i++){
        scanf("%d",&b[i]);
    }

  int max = a[0];

  int min1 = a[0];

  for (i = 0; i < n; i++)

   {

      if (a[i] > max)

        {

          max = a[i];

        }

      else if (a[i] < min1)

        {

          min1 = a[i];

        }

    }

        int min = b[0];
        for(i = 0 ;i < m ; i++){
            if(b[i] < min){
                min = b[i] ;
            }
        }

        if(max > min){
            printf("-1");

            return 0;
        }
        else if((2*min1 < min) && (2*min1 > max)){
            printf("%d",2*min1);

            return 0;
        }
        else if ( 2*min1 < max){
                printf("%d",max);

            return 0;

        }
        else {
            printf("-1");

            return 0;
        }

}


#include <stdio.h>

int main ()
{
    int n ,m ;
    int a[100];
    int i;
    int b[100];
    scanf("%d%d",&n,&m);
    for(i =0 ;i <n;i++){
            scanf("%d",&a[i]);
    }
    for(i =0 ;i <m ;i++){
        scanf("%d",&b[i]);
    }

  int max = a[0];

  int min1 = a[0];

  for (i = 0; i < n; i++)

   {

      if (a[i] > max)

        {

          max = a[i];

        }

      else if (a[i] < min1)

        {

          min1 = a[i];

        }

    }

        int min = b[0];
        for(i = 0 ;i < m ; i++){
            if(b[i] < min){
                min = b[i] ;
            }
        }

        if(max > min){
            printf("-1");

        }
        else if((2*min1 < min) && (2*min1 > max)){
            printf("%d",2*min1);

        }
        else if ( 2*min1 < max){
                printf("%d",max);

    ;

        }
        else {
            printf("-1");

        }
        return 0;

}


#include <stdio.h>

int main ()
{
    int n ,m ;
    int a[100];
    int i;
    int b[100];
    scanf("%d%d",&n,&m);
    for(i =0 ;i <n;i++){
            scanf("%d",&a[i]);
    }
    for(i =0 ;i <m ;i++){
        scanf("%d",&b[i]);
    }

  int max = a[0];

  int min1 = a[0];

  for (i = 0; i < n; i++)

   {

      if (a[i] > max)

        {

          max = a[i];

        }

      else if (a[i] < min1)

        {

          min1 = a[i];

        }

    }

        int min = b[0];
        for(i = 0 ;i < m ; i++){
            if(b[i] < min){
                min = b[i] ;
            }
        }

        if(max >= min){
            printf("-1");

        }
        else if((2*min1 < min) && (2*min1 > max)){
            printf("%d",2*min1);

        }
        else if ( 2*min1 <= max){
                printf("%d",max);

    ;

        }
        else {
            printf("-1");

        }
        return 0;

}


#include<stdio.h>

#include<math.h>

int main()
{
int amax=0,amin=101,bmin=101,i,m,n,aux;

scanf("%d%d",&n,&m);

for (i=1;i<=n;i++)
{
    scanf("%d",&aux);

    if(amax<aux)amax=aux;
    if(amin>aux)amin=aux;
}
for(i=1;i<=m;i++)
{
    scanf("%d",&aux);
    if(bmin>aux)bmin=aux;
}

if(amax<bmin&&2*amin<bmin)
{
    if(2*amin<amax)printf("%d",amax);
    else printf("%d",2*amax);return(0);
}
printf("%d",-1);

return 0;
}


#include<stdio.h>

#include<math.h>

int main()
{
int amax=0,amin=101,bmin=101,i,m,n,aux;

scanf("%d%d",&n,&m);

for (i=1;i<=n;i++)
{
    scanf("%d",&aux);

    if(amax<aux)amax=aux;
    if(amin>aux)amin=aux;
}
for(i=1;i<=m;i++)
{
    scanf("%d",&aux);
    if(bmin>aux)bmin=aux;
}

if(amax<bmin&&2*amin<bmin)
{
    if(2*amin<amax)printf("%d",amax);
    else printf("%d",2*amax);exit(0);
}
printf("%d",-1);

return 0;
}


#include<stdio.h>

#include<math.h>

int main()
{
int amax=0,amin=101,bmin=101,i,m,n,aux;

scanf("%d%d",&n,&m);

for (i=1;i<=n;i++)
{
    scanf("%d",&aux);

    if(amax<aux)amax=aux;
    if(amin>aux)amin=aux;
}
for(i=1;i<=m;i++)
{
    scanf("%d",&aux);
    if(bmin>aux)bmin=aux;
}

if(amax<bmin&&2*amin<bmin)
{
    if(2*amin<amax)printf("%d",amax);
    else printf("%d",2*amin);return(0);
}
printf("%d",-1);

return 0;
}


#include<stdio.h>
int main()
{
	int n, m, j, mx = 0, mn = 0;
	int a[100], b[100];
	scanf("%d%d", &n, &m);
	for (j = 0; j < n; j++)
		scanf("%d", a + j);
	for (j = 0; j < m; j++)
		scanf("%d", b + j);
	for (j = 1; j < n; j++)
		mx = (a[mx] < a[j] ? j : mx);
	int id = -1;
	for (j = 0; j < n; j++)
	{
		if (j == mx)
			continue;
		if (a[j] <= (a[mx] / 2))
			id = j;
	}
	if (id < 0)
	{
		printf("-1\n");
		return 0;
	}
	for (j = 0; j < m; j++)
	{
		if (b[j] <= a[mx])
		{
			printf("-1\n");
			return 0;
		}
	}
	printf("%d\n", a[mx]);
	return 0;
}


#include<stdio.h>
int main()
{
	int n, m, j, mx = 0;
	int a[100], b[100];
	scanf("%d%d", &n, &m);
	for (j = 0; j < n; j++)
		scanf("%d", a + j);
	for (j = 0; j < m; j++)
		scanf("%d", b + j);
	for (j = 1; j < n; j++)
		mx = (a[mx] < a[j] ? j : mx);
	for (j = 0; j < n; j++)
	{
		if (j == mx)
			continue;
		if (a[j] <= (a[mx] / 2))
			break;
	}
	if (j == n)
	{
		if (n > 1)
		{
			printf("-1\n");
			return 0;
		}
	}
	for (j = 0; j < m; j++)
	{
		if (b[j] <= a[mx])
		{
			printf("-1\n");
			return 0;
		}
	}
	printf("%d\n", a[mx]);
	return 0;
}


#include<stdio.h>
int comp(const void* a1, const void* b1)
{
	return *((const int*)a1)- *((const int*)b1);
}
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	int a[n],b[m];
	int i=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=0;i<m;i++)
	{
		scanf("%d",&b[i]);
	}
	int v=0;
	qsort(a,sizeof(a)/sizeof(int),sizeof(int),comp);
	qsort(b,sizeof(b)/sizeof(int),sizeof(int),comp);
	if( (a[n-1] < b[0]) && (a[n-1] > 0) )
	{
		if(a[n-1] >= 2*a[0])
		{
			v = a[n-1];
			printf("%d\n",v);
		}
		else
		{
			printf("-1\n");
		}
	}
	else
	{
		printf("-1\n");
	}
	return 0;
}


#include<stdio.h>
int comp(const void* a1, const void* b1)
{
	return *((const int*)a1)- *((const int*)b1);
}
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	int a[n],b[m];
	int i=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=0;i<m;i++)
	{
		scanf("%d",&b[i]);
	}
	int v=0;
	qsort(a,sizeof(a)/sizeof(int),sizeof(int),comp);
	qsort(b,sizeof(b)/sizeof(int),sizeof(int),comp);
	if( (a[n-1] < b[0]) && (a[n-1] > 0) )
	{
		if(2*a[0] <= b[0])
		{
			if(2*a[0]<= a[n-1])
			{
				v = a[n-1];
			}
			else
			{
				v = 2*a[0];
			}
			printf("%d\n",v);
		}
		else
		{
			printf("-1\n");
		}
	}
	else
	{
		printf("-1\n");
	}
	return 0;
}


#include<stdio.h>
int comp(const void* a1, const void* b1)
{
	return *((const int*)a1)- *((const int*)b1);
}
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	int a[n],b[m];
	int i=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=0;i<m;i++)
	{
		scanf("%d",&b[i]);
	}
	int v=0;
	qsort(a,sizeof(a)/sizeof(int),sizeof(int),comp);
	qsort(b,sizeof(b)/sizeof(int),sizeof(int),comp);
	if( (a[n-1] < b[0]) && (a[n-1] > 0) )
	{
		if(2*a[0] < b[0])
		{
			if(2*a[0]<= a[n-1])
			{
				v = a[n-1];
			}
			else
			{
				v = 2*a[0];
			}
			printf("%d\n",v);
		}
		else
		{
			printf("-1\n");
		}
	}
	else
	{
		printf("-1\n");
	}
	return 0;
}


#include<stdio.h>
int ara[10000];
int main()
{
  int a,b,r_t,max,i,w_t,min,min1,time;
  while(~scanf("%d %d",&a,&b))
  {
    max=0;
    min=10000;
    min1=10000;
    for(i=0;i<a;i++)
    {
      scanf("%d",&r_t);
      if(max<r_t)max=r_t;
      if(min>r_t)min=r_t;
    }
    for(i=0;i<b;i++)
    {
      scanf("%d",&w_t);
      if(min1>w_t)min1=w_t;
    }
    if((2*min)<min1 && max<min1)
    {
      if((2*min)<=max)
      {
        time=max;
      }
      else if((2*min)>max)
      {
        time=(2*min);
      }
      printf("%d\n",time);
    }
    else
    {
      printf("-1\n");
    }
  }
  return 0;
}


#include<stdio.h>
int ara[10000];
int main()
{
  int a,b,r_t,max,i,w_t,min,min1,time;
  while(~scanf("%d %d",&a,&b))
  {
    max=0;
    min=100000;
    min1=100000;
    for(i=0;i<a;i++)
    {
      scanf("%d",&r_t);
      if(max<r_t)max=r_t;
      if(min>r_t)min=r_t;
    }
    for(i=0;i<b;i++)
    {
      scanf("%d",&w_t);
      if(min1>w_t)min1=w_t;
    }
    if((2*min)<min1 && max<min1)
    {
      if((2*min)<max)
      {
        time=max;
      }
      else if((2*min)>max)
      {
        time=(2*min);
      }
      printf("%d\n",time);
    }
    else
    {
      printf("-1\n");
    }
  }
  return 0;
}


#include<stdio.h>
int ara[10000];
int main()
{
  int a,b,r_t,max,i,w_t,min,min1,time;
  while(~scanf("%d %d",&a,&b))
  {
    max=0;
    min=10000;
    min1=10000;
    for(i=0;i<a;i++)
    {
      scanf("%d",&r_t);
      if(max<r_t)max=r_t;
      if(min>r_t)min=r_t;
    }
    for(i=0;i<b;i++)
    {
      scanf("%d",&w_t);
      if(min1>w_t)min1=w_t;
    }
    if((2*min)<=min1 && max<min1)
    {
      if((2*min)<max)
      {
        time=max;
      }
      else if((2*min)>max)
      {
        time=(2*min);
      }
      printf("%d\n",time);
    }
    else
    {
      printf("-1\n");
    }
  }
  return 0;
}


#include<stdio.h>

int main()
{
    int n, m, max = 0, min = 101;
    scanf("%d%d", &n, &m);
    int right[n], wrong, i, j;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &right[i]);
        if(max < right[i])
            max = right[i];
        if(min > right[i])
            min = right[i];
    }
    int index = 2;
    for(i = 0; i < n; i++)
    {
        if(2 * right[i] <= max)
        {
            index = 1;
            break;
        }
    }
    if(index != 1)
        max = 2 * min;
    for(j = 0; j < m; j++)
    {
        scanf("%d", &wrong);
        if(max >= wrong)
            index = 0;
    }
    if(index == 0)
        printf("-1");
    else
        printf("%d", max);
    return 0;
}


#include<stdio.h>

int main()
{
    int n, m, max = 0;
    scanf("%d%d", &n, &m);
    int right[n], wrong, i, j;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &right[i]);
        if(max < right[i])
        max = right[i];
    }
    int index = 2;
    for(j = 0; j < m; j++)
    {
        scanf("%d", &wrong);
        if(max > wrong)
            index = 0;
    }
    if(index == 0)
    {
        printf("-1");
        return 0;
    }
    for(i = 0; i < n; i++)
    {
        if(2 * right[i] <= max)
        {
            index = 1;
            break;
        }
    }
    if(index == 1)
        printf("%d", max);
    else
        printf("-1");
    return 0;
}


#include<stdio.h>

int main()
{
    int n, m, max = 0;
    scanf("%d%d", &n, &m);
    int right[n], wrong, i, j;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &right[i]);
        if(max < right[i])
        max = right[i];

    }
    for(j = 0; j < m; j++)
        scanf("%d", &wrong);
    int index;
    for(i = 0; i < n; i++)
    {
        if(2 * right[i] <= max)
        {
            index = 1;
            break;
        }
    }
    if(index == 1)
        printf("%d", max);
    else
        printf("-1");
    return 0;
}


#include<stdio.h>

int main()
{
    int n, m, max = 0;
    scanf("%d%d", &n, &m);
    int right[n], wrong, i, j;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &right[i]);
        if(max < right[i])
        max = right[i];

    }
    for(j = 0; j < m; j++)
        scanf("%d", &wrong);
    int index = 0;
    for(i = 0; i < n; i++)
    {
        if(2 * right[i] <= max)
        {
            index = 1;
            break;
        }
    }
    if(index == 1)
        printf("%d", max);
    else
        printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,max=0,cmin=110,wmin=110,i,ans=0,cara[110]= {0},wara[110]= {0};
    scanf("%d %d",&n,&m);
    for(i=0; i<n; i++)
    {
        scanf("%d",&cara[i]);
        if(cara[i]>max)
            max=cara[i];
        if(cara[i]<cmin)
            cmin=cara[i];
    }
    for(i=0; i<m; i++)
    {
        scanf("%d",&wara[i]);
        if(wara[i]<wmin)
            wmin=wara[i];
    }
    ans=(2*cmin>max)?(2*cmin):max;
    if(ans<wmin)
    {
        printf("%d\n",ans);
    }

    else
        printf("-1\n");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m,max=0,cmin=110,wmin=110,i,j,cara[110]={0},wara[110]={0};
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
        scanf("%d",&cara[i]);
        if(cara[i]>max)
            max=cara[i];
        if(cara[i]<cmin)
            cmin=cara[i];
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&wara[i]);
        if(wara[i]<wmin)
            wmin=wara[i];
    }
    if(max<wmin && 2*cmin<=max)
    {
            printf("%d\n",max);
    }
    else
        printf("-1\n");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[n];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
   for (i=mina*2;i<minb;i++){
        if (i>=maxa){
            break;
        }
   }
   if (i<minb){
    printf("%d",i);
   }else
    printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[n];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
    if (mina*2<maxa&&minb>maxa){
        printf("%d",maxa);
    }else
        printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[k];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
    if (mina*2<=maxa&&maxa<minb){
        printf("%d",maxa);
    }else if (mina*2<minb&&mina*2>=maxa){
        printf("%d",mina*2);
    }else
        printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[n];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
    if (mina*2<=maxa&&maxa<minb){
        printf("%d",maxa);
    }else if (mina*2<minb&&mina*2>=maxa){
        printf("%d",mina*2);
    }else
        printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[n];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
   for (i=mina*2;i<minb;i++){
        if (i>=maxa){
            break;
        }
   }
   if (i>=minb){
    printf("%d",i);
   }else
    printf("-1");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n];
    int b[n];
    int i;
    int mina=INT_MAX;
    int maxa=INT_MIN;
    int minb=INT_MAX;
    for (i=0;i<n;i++){
        scanf("%d",&a[i]);
        if (a[i]<mina){
            mina=a[i];
        }
        if (a[i]>maxa){
            maxa=a[i];
        }
    }
    for (i=0;i<k;i++){
        scanf("%d",&b[i]);
        if (b[i]<minb){
            minb=b[i];
        }
    }
   for (i=mina*2;i<minb;i++){
        if (i>=maxa){
            break;
        }
   }
   if (i!=minb){
    printf("%d",i);
   }else
    printf("-1");
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
    return(*(int *)a)>(*(int *)b)?1:-1;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,s,i;
    int p[105];
    int q[105];
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
       scanf("%d",&p[i]);
    for(i=0;i<m;i++)
       scanf("%d",&q[i]);
    qsort(p,n,sizeof(int),cmp);
    qsort(q,m,sizeof(int),cmp);
    s=p[n-1];
    if(s<q[0])
    {
        if(2*p[0]<=s)
            printf("%d\n",s);
        else if(p[0]==s)
        {
            s=2*s;
            if(s<q[0])
               printf("%d\n",s);
            else
               printf("-1\n");
        }
        else
            printf("-1\n");
    }
    else
        printf("-1\n");
    return 0;
}


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    bool d = false;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = max(0ll, (need - r + k - 1) / k);
      if (reload > m[j].x.y - m[j].x.x) {
        if (d) {
          r = k;
          reload = max(0ll, (need - r + k - 1) / k);
          if (reload > m[j].x.y - m[j].x.x || m[j - 1].x.y != m[j].x.x) {
            c = oo;
          } else {
            c += reload + 1;
            r += k * reload;
            r -= need;
            reload -= 1;
          }
        } else {
          c = oo;
        }
      } else {
        c += reload;
        r += k * reload;
        r -= need;
      }
      if (c >= oo) {
        c = oo, r = -oo;
      }
      cost[i][j] = c;
      rest[i][j] = r;
      if (reload < m[j].x.y - m[j].x.x)
        d = true;
      else
        d = false;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo || dp[j] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 20000;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = m[j].x.y - m[j].x.x;
      if (reload * k + r < need) {
        c = oo;
      } else {
        reload = max(0ll, (need - r + k - 1) / k);
        c += reload;
        r += k * reload;
        r -= need;
      }
      cmin(c, oo);
      cost[i][j] = c;
      rest[i][j] = r;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    bool d = false;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = max(0ll, (need - r + k - 1) / k);
      if (reload > m[j].x.y - m[j].x.x) {
        if (d) {
          if (reload > m[j].x.y - m[j].x.x + 1) {
            c = oo;
          } else {
            c += reload;
            r += k * reload;
            r -= need;
          }
        } else {
          c = oo;
        }
      } else {
        c += reload;
        r += k * reload;
        r -= need;
      }
      if (c >= oo) {
        c = oo, r = -oo;
      }
      cost[i][j] = c;
      rest[i][j] = r;
      if (reload < m[j].x.y - m[j].x.x)
        d = true;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo || dp[j] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = m[j].x.y - m[j].x.x;
      if (reload * k + r < need) {
        c = oo;
      } else {
        reload = max(0ll, (need - r + k - 1) / k);
        c += reload;
        r += k * reload;
        r -= need;
      }
      cmin(c, oo);
      cost[i][j] = c;
      rest[i][j] = r;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }