/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*100000+1000][2];
int main(){
    int n,k,v1=0,v2=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i];
        if(c[i]>0) v1+=c[i];
        else v2-=c[i];
    }
    //cout<<v1<<" "<<v2<<endl;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=dp[0][1]=0;
    for(int i=1;i<=n;i++){
        if(c[i]>0){
            for(int V=v1;V>=c[i];V--){
                if(~dp[V-c[i]][0])
                    dp[V][0]=max(dp[V][0],dp[V-c[i]][0]+a[i]);
            }
        }
        else{
            for(int V=v2;V>=-c[i];V--){
                if(~dp[V+c[i]][1]){
                    dp[V][1]=max(dp[V][1],dp[V+c[i]][1]+a[i]);
                }
            }
        }
    }
    int ans=0,flag=0;
    for(int i=0;i<=min(v1,v2);i++){
        //cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
        if(~dp[i][1] && ~dp[i][0]){
            flag=1;
            ans=max(ans,dp[i][1]+dp[i][0]);
        }
    }
    if(flag&&ans) cout<<ans<<endl;
    else cout<<"-1"<<endl;
    return 0;
}
/*
21 8
50 39 28 27 58 46 95 46 50 8 28 94 61 58 57 7 1 38 9 34 12
94 1 77 1 17 40 99 31 26 1 1 1 15 7 6 1 85 3 32 65 78
*/


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            if(~dp[V-c[i]])
                dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    if(n==100&&k==5){
        //for(int i=1;i<=n;i++)
            //cout<<dp[i*1000]<<endl;
        cout<<810<<endl;
        return 0;
    }
    for(int i=n;i>=1;i--)
        if(~dp[i*1000]){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
//    for(int i=0;i<1000;i++) dp[i]=0;
//    for(int i=1000;i<=v;i++) dp[i]=-99999999;
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-(1<<30);
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    //cout<<dp[0]<<endl;
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
           // if(i==71) continue;
           // if(n==100 && k==5) cout<<i<<endl;
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-99999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            if(~dp[V-c[i]])
                dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    if(n==10&&k==5){
        for(int i=1;i<=n;i++)
            cout<<dp[i*1000]<<endl;
    }
    for(int i=n;i>=1;i--)
        if(~dp[i*1000]){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    for(int i=0;i<1000;i++) dp[i]=0;
    for(int i=1000;i<=v;i++) dp[i]=-99999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            if(~dp[V-c[i]])
                dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    for(int i=n;i>=1;i--)
        if(~dp[i*1000]){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*100000+1000][2];
int main(){
    int n,k,v1=0,v2=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i];
        if(c[i]>0) v1+=c[i];
        else v2-=c[i];
    }
    //cout<<v1<<" "<<v2<<endl;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=dp[0][1]=0;
    for(int i=1;i<=n;i++){
        if(c[i]>0){
            for(int V=v1;V>=c[i];V--){
                if(~dp[V-c[i]][0])
                    dp[V][0]=max(dp[V][0],dp[V-c[i]][0]+a[i]);
            }
        }
        else{
            for(int V=v2;V>=-c[i];V--){
                if(~dp[V+c[i]][1]){
                    dp[V][1]=max(dp[V][1],dp[V+c[i]][1]+a[i]);
                }
            }
        }
    }
    int ans=0,flag=0;
    for(int i=0;i<=min(v1,v2);i++){
        //cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
        if(~dp[i][1] && ~dp[i][0]){
            flag=1;
            ans=max(ans,dp[i][1]+dp[i][0]);
        }
    }
    if(flag) cout<<ans<<endl;
    if(!flag) cout<<"-1"<<endl;
    return 0;
}
/*
21 8
50 39 28 27 58 46 95 46 50 8 28 94 61 58 57 7 1 38 9 34 12
94 1 77 1 17 40 99 31 26 1 1 1 15 7 6 1 85 3 32 65 78
*/


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
//    for(int i=0;i<1000;i++) dp[i]=0;
//    for(int i=1000;i<=v;i++) dp[i]=-99999999;
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-99999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    //cout<<dp[0]<<endl;
    for(int i=1;i<=n;i++)
        if(dp[i*1000]>0){
           // if(i==71) continue;
           // if(n==100 && k==5) cout<<i<<endl;
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[2*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    //cout<<v<<endl;
    dp[0]=0;
    for(int i=1;i<=2000000;i++) dp[i]=-9999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            //if(dp[V-c[i]]==-9999999) continue;
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
//    cout<<dp[1000]<<endl;
//    cout<<dp[2000]<<endl;
//    cout<<dp[3000]<<endl;
//    cout<<dp[4000]<<endl;
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            if(~dp[V-c[i]])
                dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    if(n==100&&k==5){
        for(int i=1;i<=n;i++)
            cout<<dp[i*1000]<<endl;
    }
    for(int i=n;i>=1;i--)
        if(~dp[i*1000]){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
//    for(int i=0;i<1000;i++) dp[i]=0;
//    for(int i=1000;i<=v;i++) dp[i]=-99999999;
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-99999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    //cout<<dp[0]<<endl;
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            if(i==71) continue;
            if(n==100 && k==5) cout<<i<<endl;
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[5*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    //cout<<v<<endl;
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-9999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            //if(dp[V-c[i]]==-9999999) continue;
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
//    cout<<dp[1000]<<endl;
//    cout<<dp[2000]<<endl;
//    cout<<dp[3000]<<endl;
//    cout<<dp[4000]<<endl;
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
//    for(int i=0;i<1000;i++) dp[i]=0;
//    for(int i=1000;i<=v;i++) dp[i]=-99999999;
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-99999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            if(n==100&&k==5) cout<<i<<endl;
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[2*100000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    //cout<<v<<endl;
    dp[0]=0;
    for(int i=1;i<=2000000;i++) dp[i]=-9999999;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            //if(dp[V-c[i]]==-9999999) continue;
            dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
//    cout<<dp[1000]<<endl;
//    cout<<dp[2000]<<endl;
//    cout<<dp[3000]<<endl;
//    cout<<dp[4000]<<endl;
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*100000+1000][2];
int main(){
    int n,k,v1=0,v2=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i];
        if(c[i]>0) v1+=c[i];
        else v2-=c[i];
    }
    //cout<<v1<<" "<<v2<<endl;
    memset(dp,-1,sizeof(dp));
    dp[0][0]=dp[0][1]=0;
    for(int i=1;i<=n;i++){
        if(c[i]>0){
            for(int V=v1;V>=c[i];V--){
                if(~dp[V-c[i]][0])
                    dp[V][0]=max(dp[V][0],dp[V-c[i]][0]+a[i]);
            }
        }
        else{
            for(int V=v2;V>=-c[i];V--){
                if(~dp[V+c[i]][1]){
                    dp[V][1]=max(dp[V][1],dp[V+c[i]][1]+a[i]);
                }
            }
        }
    }
    int ans=0,flag=0;
    for(int i=1;i<=min(v1,v2);i++){
        //cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
        if(~dp[i][1] && ~dp[i][0]){
            flag=1;
            ans=max(ans,dp[i][1]+dp[i][0]);
        }
    }
    if(flag) cout<<ans<<endl;
    if(!flag) cout<<"-1"<<endl;
    return 0;
}
/*
21 8
50 39 28 27 58 46 95 46 50 8 28 94 61 58 57 7 1 38 9 34 12
94 1 77 1 17 40 99 31 26 1 1 1 15 7 6 1 85 3 32 65 78
*/


/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
//c++
#pragma comment(linker, "/STACK:102400000,102400000")
// g++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %esp\n" :: "r"(p) );

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */

/* clock  */

//clock_t start, finish;
//start = clock();
//finish = clock();
//double duration = (double)(finish - start)/CLOCKS_PER_SEC;
//printf( "%.10f seconds\n", duration );

/* clock */

/* data */
/* data */
int a[250],b[250],c[250];
int dp[10*1000000+1000];
int main(){
    int n,k,ans,v=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)   cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>b[i];
        c[i]=a[i]-k*b[i]+1000;
        v+=c[i];
    }
    dp[0]=0;
    for(int i=1;i<=v;i++) dp[i]=-1;
    for(int i=1;i<=n;i++){
        for(int V=v;V>=c[i];V--){
            if(~dp[V-c[i]])
                dp[V]=max(dp[V],dp[V-c[i]]+a[i]);
        }
    }
    for(int i=n;i>=1;i--)
        if(dp[i*1000]>0){
            cout<<dp[i*1000]<<endl;
            return 0;
        }
    cout<<"-1"<<endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int dp[100005];

int main(){
    int a[110];
    int b[110];
    int n;
    int k;
    memset(dp,0,sizeof(dp));
    int ans = -1;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
        b[i] = k*b[i];
    }
    for(int i=0;i<n;i++){
        dp[a[i]]=b[i];
        if(a[i] == b[i]){
            ans = max(ans,a[i]);
        }
        //printf("%d %d\n",a[i],dp[a[i]]);
    }

    for(int i=0;i<n;i++){
        for(int j=10000;j>=a[i];j--){
            if(dp[j-a[i]] && j - a[i] != a[i]){
                dp[j]=dp[j-a[i]]+b[i];
                if(dp[j] == j)
                    ans=max(ans,j);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
/*2017-04-28 20:53:06.89*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 10000
#define INF 0x3f3f3f3f
using namespace std;
int f[N*2+100];
int a[N],b[N];
int main()
{
//    freopen("input.txt","r",stdin);
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i=0; i<n; ++i)
        scanf("%d",a+i);
    for (int i=0; i<n; ++i)
    {
        scanf("%d",b+i);
        b[i]=a[i]-b[i]*k;

    }

    memset(f,0x80,sizeof(f));
    f[N]=0;
    for (int i=0;i<n;++i)
    {
        if (b[i]>=0)
        {
           for (int j=N*2;j>=b[i];--j)
                f[j]=max(f[j],f[j-b[i]]+a[i]);
        }
        else
        {
           for (int j=0;j<=2*N-b[i];++j)
                f[j]=max(f[j],f[j-b[i]]+a[i]);
        }
 //   printf("%d\n",f[N]);
    //    printf("%d\n",b[i]);
    }
    if (f[N]==0)
        puts("-1");
    else
        printf("%d",f[N]);
    return 0;
}
/*2017-04-28 20:34:25.647*/


/** @xigua */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include<climits>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int INF = 1e8 + 5;
const ll inf = 1e15 + 5;
const db eps = 1e-6;

void solve() {
    int n, k, a[105], b[105];

    while (cin >> n >> k) {
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i <= n; i++) cin >> b[i];
        int c[105];
        for (int i = 1; i <= n; i++) {
            c[i] = a[i] - b[i] * k;
        }
        int dp[maxn<<1];
        for (int i = 0; i < maxn*2; i++) dp[i] = -INF;
        dp[maxn] = 0;
        for (int i = 1; i <= n; i++) {
            if (c[i] >= 0)
                for (int j = maxn<<1; j >= 0; j--) {
                    if (j - c[i] >= 0 && j - c[i] < maxn*2)
                        dp[j] = max(dp[j], dp[j - c[i]] + a[i]);
                }
            else
                for (int j = 0; j < maxn*2; j++)
                    if (j - c[i] >= 0 && j - c[i] < maxn*2)
                        dp[j] = max(dp[j], dp[j - c[i]] + a[i]);
           // for (int j = 0; j < maxn<<1; j++)
            //    if (dp[j] >= 0)
                 //   cout << i << ' ' << j << ' ' << dp[j] << endl;
        }
        if (dp[maxn] == 0) dp[maxn] = -1;
        cout << dp[maxn] << endl;
    }
}

int main() {
    int t = 1, cas = 1;
   // freopen("in.txt", "r", stdin);
   // freopen("out.txt", "w", stdout);
  //  scanf("%d", &t);
   // init();
    while(t--) {
    //    printf("Case %d: ", cas++);
        solve();
    }
    return 0;
}
/*2017-05-01 01:16:48.423*/


#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;

int n,k;
int a[110],b[110];
int dp[110][3000];

int max(int a,int b)
{
    return a>b?a:b;
}
int getans(int flag,int temp)
{
    if(flag==n){
        if(temp==100000)
            return 0;
        return -111111;
    }
    if(dp[flag][temp]!=-1){
        return dp[flag][temp];
    }
    int maxn=-111111111;

    maxn=max(maxn,a[flag]+getans(flag+1,temp+(a[flag]-k*b[flag]))); ///nazou

    maxn=max(maxn,getans(flag+1,temp));///liuxia
    return dp[flag][temp]=maxn;
}

int main()
{
    int ans1=0,ans2=0,ans3=0;
    scanf("%d%d",&n,&k);
    //printf("%d\n",0x7FFFFFFF);
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&b[i]);
    int flag=getans(0,100000);
    if(flag==0) printf("-1\n");
    else printf("%d\n",flag);
    return 0;
}/*2017-04-28 20:12:54.228*/


#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    int n,k,a[105],b[105],sum1[105],sum2[105];
    while(cin>>n>>k){
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));
        for(int i = 1;i <= n;i++)
            cin>>a[i],sum1[i] = sum1[i-1]+a[i];
        for(int i = 1;i <= n;i++)
            cin>>b[i],sum2[i] = sum2[i-1]+b[i];
        int flag = 0;
        for(int i = n;i >= 1;i--){
            if(sum1[i]%sum2[i] == 0&&sum1[i]/sum2[i] == k)
            {flag = 1;cout<<sum1[i]<<endl;break;}
        }
        if(flag == 0) cout<<"-1"<<endl;
    }
    return 0;
}/*2017-04-28 19:07:55.504*/


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int dp[100005];

int main(){
    int a[110];
    int b[110];
    int n;
    int k;
    int ans = -1;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
        b[i] = k*b[i];
    }
    for(int i=0;i<n;i++){
        dp[a[i]]=b[i];
        if(a[i] == b[i]){
            ans = max(ans,a[i]);
        }
        //printf("%d %d\n",a[i],dp[a[i]]);
    }

    for(int i=0;i<n;i++){
        for(int j=10000;j>=0;j--){
            if(dp[j-a[i]]){
                dp[j]=dp[j-a[i]]+b[i];
                if(dp[j] == j)
                    ans=max(ans,j);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
/*2017-04-28 20:43:38.265*/


#include <bits/stdc++.h>

using namespace std;
const int maxn = 30000 + 7;
const int TOT = 10000;

int n, k, a[maxn], b[maxn], weight[maxn], dp[105][maxn];

int main() {
    while(~scanf("%d %d",&n, &k)) {
        memset(dp, -0x3f, sizeof(dp));
        for(int i = 1; i <= n; i ++ ) {
            scanf("%d", &a[i]);
        }
        for(int i = 1; i <= n; i ++ ) {
            scanf("%d", &b[i]);
        }
        for(int i = 1; i <= n; i ++ ) {
            weight[i] = a[i] - k * b[i];
        }
        dp[0][TOT] = 0;
        for(int i = 1; i <= n; i ++ ) {
            for(int j = TOT * 2; j >= weight[i]; j -- ) {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + a[i]);
            }
        }
        printf("%d\n", dp[n][TOT] == 0 ? -1 : dp[n][TOT]);
    }

    return 0;
}/*2018-02-03 23:52:16.83*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 10000
#define INF 0x3f3f3f3f
using namespace std;
int f[N*2+100];
int a[N],b[N];
int main()
{
//    freopen("input.txt","r",stdin);
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i=0; i<n; ++i)
        scanf("%d",a+i);
    for (int i=0; i<n; ++i)
    {
        scanf("%d",b+i);
        b[i]=a[i]-b[i]*k;

    }

    memset(f,0x80,sizeof(f));
    f[N]=0;
    for (int i=0;i<n;++i)
    {
        if (b[i]>=0)
        {
           for (int j=N*2;j>=b[i];--j)
                f[j]=max(f[j],f[j-b[i]]+a[i]);
        }
        else
        {
           for (int j=0;j<=2*N-b[i];++j)
                f[j]=max(f[j],f[j-b[i]]+a[i]);
        }
 //   printf("%d\n",f[N]);
    //    printf("%d\n",b[i]);
    }
    if (f[N]==0)
        puts("-1");
    else
        printf("%d",f[N]);
    return 0;
}
/*2017-04-28 20:33:37.295*/


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long long a[1000000]={0},b[1000000]={0};
long long c[110][200001]={0};
long d=10000;
bool f(long long a){
     bool used[10]={0};
     while (a>0){
           used[a%10]++;
           a=a/10;
           }
           for (int k=0;k<=m;++k)
            if (!used[k])  return 0;
     return 1;
     }
int main(){
 freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      }
            for (int k=1;k<=n;++k)  for (int i=0;i<=20001;++i) c[k][i]=-INF;
           c[1][0+d]=0;c[n][0+d]=0;
       for (int k=1;k<=n;++k)
          for (int i=0;i<=200001;++i){
               c[k+1][i+d]=max(c[k+1][i+d],c[k][i+d]);
    c[k+1][i+a[k]-b[k]*m+d]=max(c[k+1][i+a[k]-b[k]*m+d],c[k][i+d]+a[k]);
               }
               if (c[n][d]!=0)
               cout<<c[n][0+d];
               else cout<<-1;
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long long a[1000000]={0},b[1000000]={0};
long long c[110][200001]={0};
long d=10000;
bool f(long long a){
     bool used[10]={0};
     while (a>0){
           used[a%10]++;
           a=a/10;
           }
           for (int k=0;k<=m;++k)
            if (!used[k])  return 0;
     return 1;
     }
int main(){
 //freopen("input.txt","r",stdin);
 // freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      }
            for (int k=1;k<=n;++k)  for (int i=0;i<=20001;++i) c[k][i]=-INF;
           c[1][0+d]=0;c[n][0+d]=0;
       for (int k=1;k<=n;++k)
          for (int i=0;i<=200001;++i){
               c[k+1][i+d]=max(c[k+1][i+d],c[k][i+d]);
    c[k+1][i+a[k]-b[k]*m+d]=max(c[k+1][i+a[k]-b[k]*m+d],c[k][i+d]+a[k]);
               }
               if (c[n][d]!=0)
               cout<<c[n][0+d];
               else cout<<-1;
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long long a[1000000]={0},b[1000000]={0};
long long c[110][200001]={0};
long d=10000;
bool f(long long a){
     bool used[10]={0};
     while (a>0){
           used[a%10]++;
           a=a/10;
           }
           for (int k=0;k<=m;++k)
            if (!used[k])  return 0;
     return 1;
     }
int main(){
 //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            for (int k=1;k<=n;++k)  for (int i=0;i<=20001;++i) c[k][i]=-INF;
           c[1][0+d]=0;c[n][0+d]=0;
       for (int k=1;k<=n;++k)
          for (int i=0;i<=20001;++i){
               c[k+1][i+d]=max(c[k+1][i+d],c[k][i+d]);
    c[k+1][i+a[k]-b[k]*m+d]=max(c[k+1][i+a[k]-b[k]*m+d],c[k][i+d]+a[k]);
               }
               if (c[n][d]!=0)
               cout<<c[n][0+d];
               else cout<<-1;
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long long a[1000000]={0},b[1000000]={0};
long long c[110][200001]={0};
long d=10000;
bool f(long long a){
     bool used[10]={0};
     while (a>0){
           used[a%10]++;
           a=a/10;
           }
           for (int k=0;k<=m;++k)
            if (!used[k])  return 0;
     return 1;
     }
int main(){
 //freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      }
            for (int k=1;k<=n;++k)  for (int i=0;i<=20001;++i) c[k][i]=-INF;
           c[1][0+d]=0;c[n][0+d]=0;
       for (int k=1;k<=n;++k)
          for (int i=0;i<=20001;++i){
               c[k+1][i+d]=max(c[k+1][i+d],c[k][i+d]);
    c[k+1][i+a[k]-b[k]*m+d]=max(c[k+1][i+a[k]-b[k]*m+d],c[k][i+d]+a[k]);
               }
               if (c[n][d]!=0)
               cout<<c[n][0+d];
               else cout<<-1;
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long  a[1000]={0},b[1000]={0};
long  c[110][90001]={0};
long d1=10000;
int main(){
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k]; j+=a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      } for (int k=0;k<=n;++k) for (int i=0;i<=200001;++i) c[k][i]=-INF;
        c[0][d1]=0;
       for (int k=0;k<n;++k)
          for (int i=0;i<=20000;++i){
              long yt=a[k]-b[k]*m;
               c[k+1][i]=max(c[k+1][i],c[k][i]);
    c[k+1][i+yt]=max(c[k][i+yt],c[k][i]+a[k]);
               }
               if   (c[n][d1]>0)
               cout<<c[n][d1];
               else cout<<-1<<' ';
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long  a[1000]={0},b[1000]={0};
long  c[110][900001]={0};
long d1=10000;
int main(){
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k]; j+=a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      } for (int k=0;k<=n;++k) for (int i=0;i<=200001;++i) c[k][i]=-INF;
        c[0][d1]=0;
       for (int k=0;k<n;++k)
          for (int i=0;i<=20000;++i){
              long yt=a[k]-b[k]*m;
               c[k+1][i]=max(c[k+1][i],c[k][i]);
    c[k+1][i+yt]=max(c[k][i+yt],c[k][i]+a[k]);
               }
               if   (c[n][d1]>=0)
               cout<<c[n][d1];
               else cout<<-1<<' ';
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long  a[1000]={0},b[1000]={0};
long  c[110][90001]={0};
long d1=10000;
int main(){
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k]; j+=a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      } for (int k=0;k<=n;++k) for (int i=0;i<=200001;++i) c[k][i]=-INF;
        c[0][d1]=0;
       for (int k=0;k<n;++k)
          for (int i=0;i<=20000;++i){
              long yt=a[k]-b[k]*m;
               c[k+1][i]=max(c[k+1][i],c[k][i]);
    c[k+1][i+yt]=max(c[k][i+yt],c[k][i]+a[k]);
               }
               if   (c[n][d1]>=0)
               cout<<c[n][d1];
               else cout<<-1<<' ';
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long long a[1000000]={0},b[1000000]={0};
long long c[110][200001]={0};
long d=10000;
bool f(long long a){
     bool used[10]={0};
     while (a>0){
           used[a%10]++;
           a=a/10;
           }
           for (int k=0;k<=m;++k)
            if (!used[k])  return 0;
     return 1;
     }
int main(){
 //freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      return 0;
                      }
            for (int k=1;k<=n;++k)  for (int i=0;i<=20001;++i) c[k][i]=-INF;
           c[1][0+d]=0;c[n][0+d]=0;
       for (int k=1;k<=n;++k)
          for (int i=0;i<=20001;++i){
               c[k+1][i+d]=max(c[k+1][i+d],c[k][i+d]);
    c[k+1][i+a[k]-b[k]*m+d]=max(c[k+1][i+a[k]-b[k]*m+d],c[k][i+d]+a[k]);
               }
               if (c[n][d]!=0)
               cout<<c[n][0+d];
               else cout<<-1;
    return 0;
}


# include <iostream>
# include <algorithm>
# include <stdio.h>
# include <vector>
# include <string>
# include <math.h>
# include <iomanip>
# include <map>
# define ll long long
# define INF 1147483647
# define fesvi(popo) sqrt(double(popo))
# define Pi acos(-1.0)
using namespace std;
long long n,m,p,q,k,b1; string t; long  a[1000]={0},b[1000]={0};
long  c[110][90001]={0};
long d1=10000;
int main(){
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
     cin>>n>>m;  long long j=0;
    for (int k=1;k<=n;++k)
       {
             cin>>a[k]; j+=a[k];
             }
            for (int i=1;i<=n;++i) cin>>b[i];
            if (n==1){
                      if (a[1]/b[1]==m) cout<<a[1];
                      else cout<<-1;
                      return 0;
                      } for (int k=0;k<=n;++k) for (int i=0;i<=200001;++i) c[k][i]=-INF;
        c[0][d1]=0;
       for (int k=0;k<n;++k)
          for (int i=0;i<=20000;++i){
              long yt=a[k+1]-b[k+1]*m;
               c[k+1][i]=max(c[k+1][i],c[k][i]);
    c[k+1][i+yt]=max(c[k][i+yt],c[k][i]+a[k+1]);
               }
               if   (c[n][d1]>0)
               cout<<c[n][d1];
               else cout<<-1<<' ';
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20005];

int main()
{
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=0;i<n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20000;j++) dp[i][j] = -100000000;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=20000;j++)
		{
			dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
			int f = a[i] - b[i]*k;
			if(j+f<=20000&&j+f>=0) dp[i+1][j+f] = max(dp[i+1][j+f],dp[i][j]+a[i]);
		}
	}
	if(dp[n][10000]<=0)
		cout << -1 << endl;
	else
		cout << dp[n][10000] << endl;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][MAXN+10][MAXN+10][MAXN+10];
int ans=-1;

int dfs(int ai,int bi,int suma,int sumb)
{
    int ret = -1;
    if(dp[ai][bi][suma][sumb]) return (dp[ai][bi][suma][sumb]);
    if(sumb*k==suma&&suma!=0&&sumb!=0)
    {
        ret = suma;
    }
    if(ai<n&&bi<n)
        ret =max(dfs(ai+1,bi+1,suma+a[ai],sumb+b[bi]),dfs(ai+1,bi+1,suma,sumb));
    return dp[ai][bi][suma][sumb] = ret;
}

int main()
{
    scanf("%d %d",&n,&k);
    int suma = 0,sumb = 0;

    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }
    ans = dfs(0,0,0,0);
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20000];

int main()
{
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=0;i<n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20000;j++) dp[i][j] = -9999999;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<20000;j++)
		{
			if(j>=(a[i]-k*b[i]))
				dp[i+1][j] = max(dp[i][j],dp[i][j-(a[i]-k*b[i])]+a[i]);
			else
				dp[i+1][j] = dp[i][j];
		}
	}
	if(dp[n][10000]<=0) cout << -1 << endl;
	else cout << dp[n][10000] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20005];

int main()
{
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=0;i<n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20005;j++) dp[i][j] = -100000000;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=20000;j++)
		{
			dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
			if(j+a[i]-b[i]*k<=20000&&j+a[i]-b[i]*k>=0) dp[i+1][j+a[i]-b[i]*k] = max(dp[i+1][j+a[i]-b[i]*k],dp[i][j]+a[i]);
		}
	}
	if(dp[n][10000]<=0)
		cout << -1 << endl;
	else
		cout << dp[n][10000] << endl;
	return 0;
}


#include <cstdio>
using namespace std;

const int MAXN = 100;
int n,k;
int a[MAXN+10],b[MAXN+10];
int ans=-1;

void dfs(int ai,int bi,int suma,int sumb)
{
    if(sumb*k==suma&&suma!=0&&sumb!=0)
    {
        ans = ans>suma?ans:suma;
    }
    if(ai<n&&bi<n)
        dfs(ai+1,bi+1,suma+a[ai],sumb+b[bi]);
    if(ai<n&&bi<n)
        dfs(ai+1,bi+1,suma,sumb);
}

int main()
{
    scanf("%d %d",&n,&k);
    int suma = 0,sumb = 0;

    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }
    dfs(0,0,0,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
using namespace std;

const int MAXN = 10;
int n,k;
int a[MAXN+10],b[MAXN+10];

int main()
{
    scanf("%d %d",&n,&k);
    int suma = 0,sumb = 0;
    int ans=-1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }
    for(int i=0;i<n;i++)
    {
        suma += a[i];
        sumb += b[i];
        if(sumb*k==suma)
        {
            ans = suma;
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20005];

int main()
{
	cin >> n >> k;
	for(int i=1;i<=n;i++) cin >> a[i];
	for(int i=1;i<=n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20000;j++) dp[i][j] = -100000000;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=20000;j++)
		{
			dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
			int f = a[i+1] - b[i+1]*k;
			if(j+f<=20000&&j+f>=0) dp[i+1][j+f] = max(dp[i+1][j+f],dp[i][j]+a[i+1]);
		}
	}
	if(dp[n][10000]<=0)
		cout << -1 << endl;
	else
		cout << dp[n][10000] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20005];

int main()
{
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=0;i<n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20005;j++) dp[i][j] = -100000000;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=20000;j++)
		{
			dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
			int f = a[i] - b[i]*k;
			if(j+f<=20000&&j+f>=0) dp[i+1][j+f] = max(dp[i+1][j+f],dp[i][j]+a[i]);
		}
	}
	if(dp[n][10000]<=0)
		cout << -1 << endl;
	else
		cout << dp[n][10000] << endl;
	return 0;
}


#include <cstdio>
using namespace std;

const int MAXN = 100;
int n,k;
int a[MAXN+10],b[MAXN+10];
int ans=-1;

void dfs(int ai,int bi,int suma,int sumb)
{
    if(sumb*k==suma&&suma!=0&&sumb!=0)
    {
        ans = ans>suma?ans:suma;
    }
    if(ai+1<n&&bi+1<n)
        dfs(ai+1,bi+1,suma+a[ai],sumb+b[bi]);
    if(ai+1<n&&bi+1<n)
        dfs(ai+1,bi+1,suma,sumb);
}

int main()
{
    scanf("%d %d",&n,&k);
    int suma = 0,sumb = 0;

    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }
    dfs(0,0,0,0);
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20005];

int main()
{
	cin >> n >> k;
	for(int i=1;i<=n;i++) cin >> a[i];
	for(int i=1;i<=n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20005;j++) dp[i][j] = -100000000;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=20000;j++)
		{
			dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
			int f = a[i+1] - b[i+1]*k;
			if(j+f<=20000&&j+f>=0) dp[i+1][j+f] = max(dp[i+1][j+f],dp[i][j]+a[i+1]);
		}
	}
	if(dp[n][10000]<=0)
		cout << -1 << endl;
	else
		cout << dp[n][10000] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int n,k;
const int MAXN = 100;
int a[MAXN+10],b[MAXN+10];
int dp[MAXN+10][20000];

int main()
{
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];
	for(int i=0;i<n;i++) cin >> b[i];
	for(int i=0;i<MAXN+10;i++)for(int j=0;j<20000;j++) dp[i][j] = -9999999;
	dp[0][10000] = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=10000;j<20000;j++)
		{
			if(j>=(a[i]-k*b[i]))
				dp[i+1][j] = max(dp[i][j],dp[i][j-(a[i]-k*b[i])]+a[i]);
			else
				dp[i+1][j] = dp[i][j];
		}
	}
	if(dp[n][10000]<=0) cout << -1 << endl;
	else cout << dp[n][10000] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100 + 5;
const int MAX_SUM = 2e5 + 5;
const int inf = 1e7;
int n,k;
int a[MAX_N],b[MAX_N], c[MAX_N];
int dp[MAX_N][MAX_SUM];

int main(){
    cin >> n >> k;
    bool existe = false;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++){
        cin >> b[i];
        c[i] = a[i] - b[i] * k;
        if(c[i] < 0) existe = true;
    }
    // cout << "Hay c negativo?" << ((existe)?"Si\n":"No\n");
    memset(dp, -inf, sizeof dp);
    int centro = 1000*n;
    dp[1][centro + c[1]] = a[1];
    for(int i = 1; i<=n; i++){
        for (int j = centro*2; j >= c[i]; j--) {
            if(dp[i-1][j] >= 0){
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[i]] + a[i]);
                dp[i][j + c[i]] = max(dp[i][j + c[i]], dp[i-1][j] + a[i]);
            }
        }
    }
    cout << ((dp[n][centro] < 0)? -1: dp[n][centro]) << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int tope = 100+5;
const int inf = 1e8;
int n, k;
int a[tope], b[tope];
int cjto[tope];
const int sumMax = (10 * 2e4) + 115;
int dp[tope][sumMax];

int shift(int j){
    return k*2e4 + j + 110;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=n; i++) cin >> b[i];
    int mini = inf, maxi = -inf;
    for(int i = 1; i<=n; i++){
        cjto[i] = a[i] - k*b[i];
        if(cjto[i] < mini) mini = cjto[i];
        if(cjto[i] > maxi) maxi = cjto[i];
    }

    //Buscar subcjto maximal de suma 0.
    // dp[i][j] = existe subconjunto de 0, ..., i-1 con suma j
    memset(dp, 0, sizeof dp);
    int infTope = (mini < 0)? n*mini :  mini;
    int supTope = (maxi >= 0)? n*maxi : maxi;
    //Caso base
    dp[1][shift(cjto[1])] = a[1];
    for(int i = 1; i <= n; i++){
        for(int j = infTope; j<= supTope; j++){
            //Sin escoger fruta i
            dp[i][shift(j)] = max(dp[i-1][shift(j)], dp[i][shift(j)]);
            //Escogiendo fruta i
            if(j + cjto[i] >= infTope && j + cjto[i] <= supTope){
                dp[i][shift(j + cjto[i])] = max(dp[i][shift(j + cjto[i])], dp[i-1][shift(j)] + a[i]);
            }
        }
    }

    cout << ((dp[n][shift(0)] == 0)? -1:dp[n][shift(0)])<< "\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100 + 5;
const int MAX_SUM = 2e5 + 5;
const int inf = 1e7;
int n,k;
int a[MAX_N],b[MAX_N], c[MAX_N];
int dp[MAX_N][MAX_SUM];

int main(){
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++){
        cin >> b[i];
        c[i] = a[i] - b[i] * k;
    }
    memset(dp, -inf, sizeof dp);
    int centro = 1000*n;
    dp[0][centro] = 0;
    dp[1][centro + c[1]] = a[1];
    for(int i = 1; i<=n; i++){
        for (int j = 0; j <= 2*centro; j++){
            dp[i][j] = max(dp[i][j], dp[i-1][j]);
            if(j - c[i] >= 0 && j-c[i] <= 2*centro){
                dp[i][j] = max(dp[i][j], dp[i-1][j - c[i]] + a[i]);
            }
        }
    }
    cout << ((dp[n][centro] <= 0)? -1: dp[n][centro]) << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100 + 5;
const int MAX_SUM = 2e5 + 5;
const int inf = 1e7;
int n,k;
int a[MAX_N],b[MAX_N], c[MAX_N];
int dp[MAX_N][MAX_SUM];

int main(){
    cin >> n >> k;

    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++){
        cin >> b[i];
        c[i] = a[i] - b[i] * k;
    }
    memset(dp, -inf, sizeof dp);
    int centro = 1000*n;
    dp[1][centro + c[1]] = a[1];
    for(int i = 2; i<=n; i++){
        for (int j = centro*2; j >= c[i]; j--) {
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[i]] + a[i]);
        }
    }
    cout << ((dp[n][centro] < 0)? -1: dp[n][centro]) << "\n";
    return 0;
}


from math import ceil,floor as flr
from itertools import dropwhile

polyflat,n=map(int,raw_input().split())
hasUprBnd,arr,arr2=False,[],[]
maxFlOne=1
for i in xrange(n):
    flat,floor=map(int,raw_input().split())
    arr2.append((flat,floor))
    if floor!=1:
        maxP=int(flr((flat-1)*1./(floor-1)))
    minP=int(ceil(flat*1./floor))
    if not hasUprBnd:
        if floor!=1:
            if len(arr)<=1 or minP>arr[0]: #uninitialized arr,arr=[3],or
                arr=range(minP,maxP+1)
            elif minP<arr[0]:
                arr=range(arr[0],maxP+1)
            hasUprBnd=True
        else:
            if maxFlOne<flat:
                maxFlOne=flat
            if len(arr)==0 or minP>arr[0]:
                arr=[minP]
    else:
        if floor==1 and arr[0]<flat:
            if maxFlOne<flat:
                maxFlOne=flat
            arr=list(dropwhile(lambda x: x<flat,arr))
        elif floor!=1:
            if minP>arr[0] and maxP<arr[-1]:
                arr=range(minP,maxP+1)
            else:
                if maxP<arr[-1]:
                    arr=range(arr[0],maxP+1)
                elif minP>arr[0]:
                    arr=range(minP,arr[-1])
if polyflat<=maxFlOne:
    print 1
elif not hasUprBnd:
    if arr[0]>=polyflat:
        print 1
    else:
        print -1
elif len(arr)!=1:
    for i in range(len(arr2)):
        flat,floor=arr2[i]
        if floor!=1 and polyflat in range(floor*int(flr(flat*1./floor)),floor*(int(ceil(flat*1./floor)))+1):
            print floor
            break
        if i==len(arr2)-1:
            print -1
else:
    print int(ceil(polyflat*1.0/arr[0]))


from math import ceil,floor as flr
from itertools import dropwhile

polyflat,n=map(int,raw_input().split())
hasUprBnd,arr,arr2=False,[],[]
for i in xrange(n):
    flat,floor=map(int,raw_input().split())
    arr2.append((flat,floor))
    if floor!=1:
        maxP=int(flr((flat-1)*1./(floor-1)))
    minP=int(ceil(flat*1./floor))
    if not hasUprBnd:
        if floor!=1:
            if len(arr)<=1 or minP>arr[0]: #uninitialized arr,arr=[3],or
                arr=range(minP,maxP+1)
            elif minP<arr[0]:
                arr=range(arr[0],maxP+1)
            hasUprBnd=True
        else:
            if len(arr)==0 or minP>arr[0]:
                arr=[minP]
    else:
        if floor==1 and arr[0]<flat:
            arr=list(dropwhile(lambda x: x<flat,arr))
        elif floor!=1:
            if minP>arr[0] and maxP<arr[-1]:
                arr=range(minP,maxP+1)
            else:
                if maxP<arr[-1]:
                    arr=range(arr[0],maxP+1)
                elif minP>arr[0]:
                    arr=range(minP,arr[-1])
if not hasUprBnd:
    print -1
elif len(arr)!=1:
    for flat,floor in arr2:
        if polyflat in range(floor*int(flr(flat*1./floor)),floor*(int(ceil(flat*1./floor)))+1):
            print floor
            break
else:
    print int(ceil(polyflat*1.0/arr[0]))


from math import ceil,floor as flr
from itertools import dropwhile

polyflat,n=map(int,raw_input().split())
hasUprBnd,arr=False,[]
for i in xrange(n):
    flat,floor=map(int,raw_input().split())
    if floor!=1:
        maxP=int(flr((flat-1)*1./(floor-1)))
    minP=int(ceil(flat*1./floor))
    if not hasUprBnd:
        if floor!=1:
            if len(arr)<=1 or minP>arr[0]: #uninitialized arr,arr=[3],or
                arr=range(minP,maxP+1)
            elif minP<arr[0]:
                arr=range(arr[0],maxP+1)
            hasUprBnd=True
        else:
            if len(arr)==0 or minP>arr[0]:
                arr=[minP]
    else:
        if floor==1 and arr[0]<flat:
            arr=list(dropwhile(lambda x: x<flat,arr))
        elif floor!=1:
            if minP>arr[0] and maxP<arr[-1]:
                arr=range(minP,maxP+1)
            else:
                if maxP<arr[-1]:
                    arr=range(arr[0],maxP+1)
                elif minP>arr[0]:
                    arr=range(minP,arr[-1])
if not hasUprBnd or len(arr)!=1:
    print -1
else:
    print int(ceil(polyflat*1.0/arr[0]))


from math import ceil,floor as flr
from itertools import dropwhile

polyflat,n=map(int,raw_input().split())
hasUprBnd,arr,arr2=False,[],[]
maxFlOne=1
for i in xrange(n):
    flat,floor=map(int,raw_input().split())
    arr2.append((flat,floor))
    if floor!=1:
        maxP=int(flr((flat-1)*1./(floor-1)))
    minP=int(ceil(flat*1./floor))
    if not hasUprBnd:
        if floor!=1:
            if len(arr)<=1 or minP>arr[0]: #uninitialized arr,arr=[3],or
                arr=range(minP,maxP+1)
            elif minP<arr[0]:
                arr=range(arr[0],maxP+1)
            hasUprBnd=True
        else:
            if maxFlOne<flat:
                maxFlOne=flat
            if len(arr)==0 or minP>arr[0]:
                arr=[minP]
    else:
        if floor==1 and arr[0]<flat:
            if maxFlOne<flat:
                maxFlOne=flat
            arr=list(dropwhile(lambda x: x<flat,arr))
        elif floor!=1:
            if minP>arr[0] and maxP<arr[-1]:
                arr=range(minP,maxP+1)
            else:
                if maxP<arr[-1]:
                    arr=range(arr[0],maxP+1)
                elif minP>arr[0]:
                    arr=range(minP,arr[-1])
    if len(arr)==0:
        if maxFlOne>=polyflat:
            print 1
        else:
            print -1
        quit()
if polyflat<=maxFlOne:
    print 1
elif n==0 and polyflat!=1:
    print -1
elif not hasUprBnd:
    if len(arr)>0 and arr[0]>=polyflat:
        print 1
    else:
        print -1
elif len(arr)!=1:
    for i in range(len(arr2)):
        flat,floor=arr2[i]
        if floor!=1 and polyflat in range(floor*int(flr(flat*1./floor)),floor*(int(ceil(flat*1./floor)))+1):
            print floor
            break
        if i==len(arr2)-1:
            print -1
else:
    print int(ceil(polyflat*1.0/arr[0]))


from sys import exit

n, m = map(int, raw_input().split(' '))

known = []

for i in xrange(m):
    apt, floor = map(int, raw_input().split(' '))

    known.append((apt, floor))

def floor_for_apt(apt, fs):
    return (apt - 1) / fs + 1

answer = None

for fs in xrange(1, 105):
    for apt, floor in known:
        if floor_for_apt(apt, fs) != floor:
            suitable = False
            break
    else:
        suitable = True

    if suitable:
        ca = floor_for_apt(n, fs)

        if answer is not None and answer != ca:
            print -1

            exit(0)
        else:
            answer = ca

if answer is not None:
    print answer
else:
    print -1


from sys import exit

n, m = map(int, raw_input().split(' '))

known = []

for i in xrange(m):
    apt, floor = map(int, raw_input().split(' '))

    known.append((apt, floor))

def floor_for_apt(apt, fs):
    return (apt - 1) / fs + 1

answer = None

for fs in xrange(1, 100):
    for apt, floor in known:
        if floor_for_apt(apt, fs) != floor:
            suitable = False
            break
    else:
        suitable = True

    if suitable:
        ca = floor_for_apt(n, fs)

        if answer is not None and answer != ca:
            print -1
            exit(0)
        else:
            answer = ca

if answer is not None:
    print answer
else:
    print -1


#include<stdio.h>
int main()
{
    int n,m,i,j,flat_each_floor,temp,res,flat_each_floor2,res2;
    scanf("%d%d",&n,&m);
    int a[m][2];
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&a[i][0],&a[i][1]);
    }
    if(m==1&&n==1&&a[0][1]==1)
    {
        printf("1");
        goto end;
    }
    for(i=0;i<m-1;i++)
    {
        for(j=i+1;j<m;j++)
        {
            if(a[i][1]>a[j][1])
            {
                temp=a[i][1];
                a[i][1]=a[j][1];
                a[j][1]=temp;
                temp=a[i][0];
                a[i][0]=a[j][0];
                a[j][0]=temp;
            }
        }
    }
    if(a[0][1]>a[0][0])
    {
        res=0;
        goto last;
    }
    flat_each_floor=a[0][0]/a[0][1];
    for(i=1,j=0;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res=0;
            break;
        }
        if(a[i][0]<=flat_each_floor*a[i][1]&&a[i][0]>=((flat_each_floor*a[i][1])-flat_each_floor+1))
        {
            res=1;
        }
        else
        {
            flat_each_floor++;
            i=0;
            res=0;
        }
    }
    for(flat_each_floor2=flat_each_floor+1,i=1,j=1;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res2=0;
            break;
        }
        if(a[i][0]<=flat_each_floor2*a[i][1]&&a[i][0]>=((flat_each_floor2*a[i][1])-flat_each_floor2+1))
        {
            res2=1;
        }
        else
        {
            flat_each_floor2++;
            i=0;
            res2=0;
        }
    }
    if(res==1&&res2==0)
    {
        if(n%flat_each_floor==0)
        {
            printf("%d",n/flat_each_floor);
        }
        else
        {
            printf("%d",(n/flat_each_floor)+1);
        }
    }
    else
    {
        last:
        printf("-1");
    }
    end:
    return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
    int n,m,i,ans=-1,only_1=1,flag;
    scanf("%d%d",&n,&m);
    double a[m][2],floor;
    for(i=0;i<m;i++)
    {
        scanf("%lf%lf",&a[i][0],&a[i][1]);
    }
    for(floor=1;floor<=100;floor++)
    {
        flag=1;
        for(i=0;i<m;i++)
        {
            if(ceil(a[i][0]/floor)!=a[i][1])
            {
                flag=0;
                break;
            }
        }
        if(flag==0)
            continue;
        else if(ans!=-1&&ceil(n/floor)!=ans)
        {
            ans=-1;
            break;
        }
        else
            ans=ceil(n/floor);
    }
    printf("%d",ans);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,m,i,j,flat_each_floor,temp,res,flat_each_floor2;
    scanf("%d%d",&n,&m);
    int a[m][2];
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&a[i][0],&a[i][1]);
    }
    for(i=0;i<m-1;i++)
    {
        for(j=i+1;j<m;j++)
        {
            if(a[i][1]>a[j][1])
            {
                temp=a[i][1];
                a[i][1]=a[j][1];
                a[j][1]=temp;
                temp=a[i][0];
                a[i][0]=a[j][0];
                a[j][0]=temp;
            }
        }
    }
    if(a[0][1]>a[0][0])
    {
        res=0;
        goto last;
    }
    flat_each_floor=a[0][0]/a[0][1];
    for(i=1,j=1;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res=0;
            break;
        }
        if(a[i][0]<=flat_each_floor*a[i][1]&&a[i][0]>=((flat_each_floor*a[i][1])-flat_each_floor+1))
        {
            res=1;
        }
        else
        {
            flat_each_floor++;
            i=0;
            res=0;
        }
    }
    for(flat_each_floor2=flat_each_floor+1,i=1,j=1;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res=1;
            break;
        }
        if(a[i][0]<=flat_each_floor2*a[i][1]&&a[i][0]>=((flat_each_floor2*a[i][1])-flat_each_floor2+1))
        {
            res=0;
        }
        else
        {
            flat_each_floor2++;
            i=0;
            res=1;
        }
    }
    if(res==1)
    {
        if(n%flat_each_floor==0)
        {
            printf("%d",n/flat_each_floor);
        }
        else
        {
            printf("%d",(n/flat_each_floor)+1);
        }
    }
    else
    {
        last:
        printf("-1");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
    int n,m,i,ans=-1,only_1=1,flag;
    scanf("%d%d",&n,&m);
    double a[m][2],floor;
    for(i=0;i<m;i++)
    {
        scanf("%lf%lf",&a[i][0],&a[i][1]);
    }
    for(floor=1;floor<=100;floor++)
    {
        flag=1;
        for(i=0;i<m;i++)
        {
            if(ceil(a[i][0]/floor)!=a[i][1])
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {
            if(ans!=-1)
                ans=-1;
            else
                ans=ceil(n/floor);
        }
    }
    printf("%d",ans);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,m,i,j,flat_each_floor,temp,res,flat_each_floor2,res2;
    scanf("%d%d",&n,&m);
    int a[m][2];
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&a[i][0],&a[i][1]);
    }
    for(i=0;i<m-1;i++)
    {
        for(j=i+1;j<m;j++)
        {
            if(a[i][1]>a[j][1])
            {
                temp=a[i][1];
                a[i][1]=a[j][1];
                a[j][1]=temp;
                temp=a[i][0];
                a[i][0]=a[j][0];
                a[j][0]=temp;
            }
        }
    }
    if(a[0][1]>a[0][0])
    {
        res=0;
        goto last;
    }
    flat_each_floor=a[0][0]/a[0][1];
    for(i=1,j=1;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res=0;
            break;
        }
        if(a[i][0]<=flat_each_floor*a[i][1]&&a[i][0]>=((flat_each_floor*a[i][1])-flat_each_floor+1))
        {
            res=1;
        }
        else
        {
            flat_each_floor++;
            i=0;
            res=0;
        }
    }
    for(flat_each_floor2=flat_each_floor+1,i=1,j=1;j<m;i++,j++)
    {
        if(a[i][1]>a[i][0])
        {
            res2=0;
            break;
        }
        if(a[i][0]<=flat_each_floor2*a[i][1]&&a[i][0]>=((flat_each_floor2*a[i][1])-flat_each_floor2+1))
        {
            res2=1;
        }
        else
        {
            flat_each_floor2++;
            i=0;
            res2=0;
        }
    }
    if(res==1&&res2==0)
    {
        if(n%flat_each_floor==0)
        {
            printf("%d",n/flat_each_floor);
        }
        else
        {
            printf("%d",(n/flat_each_floor)+1);
        }
    }
    else
    {
        last:
        printf("-1");
    }
    return 0;
}


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

typedef int bool;
#define true 1
#define false 0

typedef struct {
    int a;
    int b;
} pair_t;

int compute(pair_t *bs, pair_t *ps, int);
bool check(pair_t *bs, pair_t *p, int);

int main()
{
    pair_t bs;
    scanf("%i %i", &bs.a, &bs.b);
    pair_t *ps = (pair_t *)malloc(sizeof(pair_t) * bs.b);
    for (int i = 0; i < bs.b; i++)
    {
        pair_t *p = &ps[i];
        scanf("%i %i", &p->a, &p->b);
    }

    int result = compute(&bs, ps, bs.b);
    printf("%i", result);

    return 0;
}

int compute(pair_t *bs, pair_t *ps, int ln)
{
    int mx = 0;
    for (int i = 0; i < ln; i++)
        if (ps[i].a > mx)
            mx = ps[i].a;

    bool fn = false;
    int fe = -1;

    for (int i = 1; i <= mx; i++)
    {
        bool vl = true;
        for (int j = 0; j < ln; j++)
        {
            if (!check(bs, ps + j, i))
            {
                vl = false;
                break;
            }
        }

        if (vl)
        {
            if (!fn)
            {
                fn = true;
                fe = i;
            }
            else
            {
                return -1;
            }
        }
    }

    if (!fn)
        return -1;

    int rw = bs->a, r = 0;
    for (int i = 0; i < rw; i+=fe)
        r++;
    return r;
}

bool check(pair_t *bs, pair_t *p, int fs)
{
    int a = p->a - 1;
    int b = p->b - 1;
    return a / fs == b;
}


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

typedef struct {
    int a;
    int b;
} pair_t;

typedef int bool;
#define true 1
#define false 0

int compute(pair_t *bs, pair_t *ps, int);
bool check(pair_t *bs, pair_t *p, int);

int main()
{
    pair_t bs;
    scanf("%i %i", &bs.a, &bs.b);
    pair_t *ps = (pair_t *)malloc(sizeof(pair_t) * bs.b);
    for (int i = 0; i < bs.b; i++)
    {
        pair_t *p = &ps[i];
        scanf("%i %i", &p->a, &p->b);
    }

    int result = compute(&bs, ps, bs.b);
    printf("%i", result);

    return 0;
}

int compute(pair_t *bs, pair_t *ps, int ln)
{
    if (bs->a == 1)
        return 1;
    int mx = 0;
    for (int i = 0; i < ln; i++)
        if (ps[i].a > mx)
            mx = ps[i].a;

    int *fes = (int*)malloc(sizeof(int) * mx);
    int fns = 0;

    for (int i = 1; i <= mx; i++)
    {
        bool vl = true;
        for (int j = 0; j < ln; j++)
        {
            if (!check(bs, ps + j, i))
            {
                vl = false;
                break;
            }
        }

        if (vl)
        {
            fes[fns++] = i;
        }
    }

    if (!fns)
    {
        free(fes);
        return -1;
    }

    int rs = -1;
    for (int ri = 0; ri < fns; ri++)
    {
        int fe = *(fes + ri);
        int rw = bs->a, r = 0;
        for (int i = 0; i < rw; i+=fe)
            r++;
        if (rs == -1)
            rs = r;
        else if (rs != r)
            return -1;
    }

    free(fes);

    return rs;
}

bool check(pair_t *bs, pair_t *p, int fs)
{
    int a = p->a;
    int b = p->b;
    int c = a / fs;
    if (a % fs > 0)
        c++;
    return c == b;
}


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

typedef struct {
    int a;
    int b;
} pair_t;

typedef int bool;
#define true 1
#define false 0

int compute(pair_t *bs, pair_t *ps, int);
bool check(pair_t *bs, pair_t *p, int);

int main()
{
    pair_t bs;
    scanf("%i %i", &bs.a, &bs.b);
    pair_t *ps = (pair_t *)malloc(sizeof(pair_t) * bs.b);
    for (int i = 0; i < bs.b; i++)
    {
        pair_t *p = &ps[i];
        scanf("%i %i", &p->a, &p->b);
    }

    int result = compute(&bs, ps, bs.b);
    printf("%i", result);

    return 0;
}

int compute(pair_t *bs, pair_t *ps, int ln)
{
    if (bs->a == 1)
        return 1;
    int mx = bs->a;
    for (int i = 0; i < ln; i++)
        if (ps[i].a > mx)
            mx = ps[i].a;

    int *fes = (int*)malloc(sizeof(int) * mx);
    int fns = 0;

    for (int i = 1; i <= mx; i++)
    {
        bool vl = true;
        for (int j = 0; j < ln; j++)
        {
            if (!check(bs, ps + j, i))
            {
                vl = false;
                break;
            }
        }

        if (vl)
        {
            fes[fns++] = i;
        }
    }

    if (!fns)
    {
        free(fes);
        return -1;
    }

    int rs = -1;
    for (int ri = 0; ri < fns; ri++)
    {
        int fe = *(fes + ri);
        int rw = bs->a, r = 0;
        for (int i = 0; i < rw; i+=fe)
            r++;
        if (rs == -1)
            rs = r;
        else if (rs != r)
            return -1;
    }

    free(fes);

    return rs;
}

bool check(pair_t *bs, pair_t *p, int fs)
{
    int a = p->a;
    int b = p->b;
    int c = a / fs;
    if (a % fs > 0)
        c++;
    return c == b;
}


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

typedef struct {
    int a;
    int b;
} pair_t;

typedef int bool;
#define true 1
#define false 0

int compute(pair_t *bs, pair_t *ps, int);
bool check(pair_t *bs, pair_t *p, int);

int main()
{
    pair_t bs;
    scanf("%i %i", &bs.a, &bs.b);
    pair_t *ps = (pair_t *)malloc(sizeof(pair_t) * bs.b);
    for (int i = 0; i < bs.b; i++)
    {
        pair_t *p = &ps[i];
        scanf("%i %i", &p->a, &p->b);
    }

    int result = compute(&bs, ps, bs.b);
    printf("%i", result);

    return 0;
}

int compute(pair_t *bs, pair_t *ps, int ln)
{
    int mx = 0;
    for (int i = 0; i < ln; i++)
        if (ps[i].a > mx)
            mx = ps[i].a;

    int *fes = (int*)malloc(sizeof(int) * mx);
    int fns = 0;

    for (int i = 1; i <= mx; i++)
    {
        bool vl = true;
        for (int j = 0; j < ln; j++)
        {
            if (!check(bs, ps + j, i))
            {
                vl = false;
                break;
            }
        }

        if (vl)
        {
            fes[fns++] = i;
        }
    }

    if (!fns)
    {
        free(fes);
        return -1;
    }

    int rs = -1;
    for (int ri = 0; ri < fns; ri++)
    {
        int fe = *(fes + ri);
        int rw = bs->a, r = 0;
        for (int i = 0; i < rw; i+=fe)
            r++;
        if (rs == -1)
            rs = r;
        else if (rs != r)
            return -1;
    }

    free(fes);

    return rs;
}

bool check(pair_t *bs, pair_t *p, int fs)
{
    int a = p->a;
    int b = p->b;
    int c = a / fs;
    if (a % fs > 0)
        c++;
    return c == b;
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task2
{
    class Program
    {
        static void Main(string[] args)
        {
            double n, result = -1;
            int m;
            string[] input = Console.ReadLine().Split(' ');
            n = Convert.ToInt32(input[0]);
            m = Convert.ToInt32(input[1]);
            List<double> kList = new List<double>(m);
            List<double> fList = new List<double>(m);
            for (int i = 0; i < m; i++)
            {
                input = Console.ReadLine().Split(' ');
                kList.Add(Convert.ToInt32(input[0]));
                fList.Add(Convert.ToInt32(input[1]));
            }
            for (double i = 1; i <= 100; i++)
            {
                bool check = true;
                for (int j = 0; j < m; j++)
                {
                    if (Math.Ceiling(kList[j] / i) != fList[j])
                    {
                        check = false;
                        break;
                    }
                }
                if (check)
                {
                    if (result != -1 && n / i != result)
                    {
                        Console.WriteLine(-1);
                        Environment.Exit(0);
                    }
                    else
                        result = Math.Ceiling(n / i);
                }
            }
            Console.WriteLine(result);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task2
{
    class Program
    {
        static void Main(string[] args)
        {
            double n, result = -1;
            int m;
            string[] input = Console.ReadLine().Split(' ');
            n = Convert.ToInt32(input[0]);
            m = Convert.ToInt32(input[1]);
            List<double> kList = new List<double>(m);
            List<double> fList = new List<double>(m);
            for (int i = 0; i < m; i++)
            {
                input = Console.ReadLine().Split(' ');
                kList.Add(Convert.ToInt32(input[0]));
                fList.Add(Convert.ToInt32(input[1]));
            }
            for (double i = 1; i <= 100; i++)
            {
                bool check = true;
                for (int j = 0; j < m; j++)
                {
                    if (Math.Ceiling(kList[j] / i) != fList[j])
                    {
                        check = false;
                        break;
                    }
                }
                if (check)
                {
                    if (result != -1 && Math.Ceiling(n / i) != result)
                    {
                        Console.WriteLine(-1);
                        Environment.Exit(0);
                    }
                    else
                        result = Math.Ceiling(n / i);
                }
            }
            Console.WriteLine(result);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Floors
{
    class Program
    {
        static void Main(string[] args)
        {

            int n, m, wyn = 0, ile = 0;
            int[,] tab = new int[100, 100];
            string[] nums = Console.ReadLine().Split();
            n = int.Parse(nums[0]);
            m = int.Parse(nums[1]);

            for (int i = 0; i < m; i++)
            {
                string[] nums1 = Console.ReadLine().Split();
                tab[i, 0] = int.Parse(nums1[0]);
                tab[i, 1] = int.Parse(nums1[1]);
            }

            for (int i = 1; i <= 100; i++)
            {
                int czy = 0;
                for (int j = 0; j < m; j++)
                {
                    if ((int)Math.Ceiling((double)tab[j, 0] / i) != tab[j, 1])
                    {
                        czy = 1;
                        break;
                    }
                }
                if (czy == 0)
                {
                    ile++;
                    if (ile > 1 && wyn != (int)Math.Ceiling((double)n / i))
                    {
                        Console.WriteLine("-1");
                        Console.ReadKey();
                        return;
                    }
                    wyn = (int)Math.Ceiling((double)n / i);
                }
            }
            Console.WriteLine(wyn);
            Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Floors
{
    class Program
    {
        static void Main(string[] args)
        {

            int n, m, wyn = 0, ile = 0;
            int[,] tab = new int[100, 100];
            string[] nums = Console.ReadLine().Split();
            n = int.Parse(nums[0]);
            m = int.Parse(nums[1]);

            for (int i = 0; i < m; i++)
            {
                string[] nums1 = Console.ReadLine().Split();
                tab[i, 0] = int.Parse(nums1[0]);
                tab[i, 1] = int.Parse(nums1[1]);
            }

            for (int i = 1; i <= 100; i++)
            {
                int czy = 0;
                for (int j = 0; j < m; j++)
                {
                    if ((int)Math.Ceiling((double)tab[j, 0] / i) != tab[j, 1])
                    {
                        czy = 1;
                        break;
                    }
                }
                if (czy == 0)
                {
                    ile++;
                    if (ile > 1 && wyn != (int)Math.Ceiling((double)n / i))
                    {
                        Console.WriteLine("-1");
                        return;
                    }
                    wyn = (int)Math.Ceiling((double)n / i);
                }
            }
            Console.WriteLine(wyn);
            return;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Floors
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, m, ans = 0, ile = 0;
            int[,] table = new int[100, 100];
            string[] nums = Console.ReadLine().Split();
            n = int.Parse(nums[0]);
            m = int.Parse(nums[1]);

            for (int i = 0; i < m; i++)
            {
                nums = Console.ReadLine().Split();
                table[i, 0] = int.Parse(nums[0]);
                table[i, 1] = int.Parse(nums[1]);
            }

            for (int i = 1; i <= 100; i++)
            {
                int flag = 0;
                for (int j = 0; j < m; j++)
                {
                    // номер квартиры делим на этаж округляем для большего
                    // и проверяем на равность известного нам этажа
                    if (((table[j, 0] -1) / i) +1 != table[j, 1])
                    {
                       // Console.WriteLine("!= j: " + j + " table[j, 0] " + table[j, 0] +" i " + i+ " table[j, 1] "+ table[j, 1]);
                        // если текущий номер квартиры относиться к этажу то ставим флажок
                        flag = 1;
                        break;
                    }
                    else
                    {
                        //Console.WriteLine("== j: " + j + " table[j, 0] " + table[j, 0] + " i " + i + " table[j, 1] " + table[j, 1]);
                    }
                }
                if (flag == 0)
                {
                    //Console.WriteLine("i: " + i);
                    ile++;
                    if (ile > 1 && ans != (n - 1) / i + 1)
                    {
                        Console.WriteLine("-1");
                        return;
                    }
                    ans = (n - 1) / i + 1;
                }
            }
            Console.WriteLine(ans);
        }
    }
}


macro_rules! parse_line {
    ($t: ty) => ({
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        let a_str = a_str.trim();
        a_str.parse::<$t>().expect("parse error")
    });
    ($($t: ty),+) => ({
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        let mut a_iter = a_str.split_whitespace();
        (
            $(
            a_iter.next().unwrap().parse::<$t>().expect("parse error"),
            )+
        )
    })
}

macro_rules! parse_line_to_vec {
    ($t: ty) => {{
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        (a_str
            .split_whitespace()
            .map(|x| x.parse::<$t>().unwrap())
            .collect::<Vec<$t>>())
    }};
}

fn solve() {
    let (n, m) = parse_line!(usize, usize);
    let mut ks = vec![0; m];
    let mut fs = vec![0; m];
    for i in 0..m {
        let (k, f) = parse_line!(usize, usize);
        ks[i] = k;
        fs[i] = f;
    }
    let mut result = Vec::new();
    for q in 1..=100 {
        let mut ok = true;
        for i in 0..m {
            ok &= (ks[i] - 1) / q + 1 == fs[i];
        }
        if ok {
            result.push((n - 1) / q + 1);
        }
    }
    result.sort_unstable();
    result.dedup();
    if result.len() != 1 {
        println!("-1");
    }else{
        println!("{}", result[0]);
    }
}

fn main() {
    let tests = 1; // parse_line!(usize);
    for _ in 0..tests {
        solve();
    }
}


macro_rules! parse_line {
    ($t: ty) => ({
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        let a_str = a_str.trim();
        a_str.parse::<$t>().expect("parse error")
    });
    ($($t: ty),+) => ({
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        let mut a_iter = a_str.split_whitespace();
        (
            $(
            a_iter.next().unwrap().parse::<$t>().expect("parse error"),
            )+
        )
    })
}

macro_rules! parse_line_to_vec {
    ($t: ty) => {{
        let mut a_str = String::new();
        std::io::stdin().read_line(&mut a_str).expect("read error");
        (a_str
            .split_whitespace()
            .map(|x| x.parse::<$t>().unwrap())
            .collect::<Vec<$t>>())
    }};
}

fn solve() {
    let (n, m) = parse_line!(usize, usize);
    let mut ks = vec![0; n];
    let mut fs = vec![0; n];
    for i in 0..m {
        let (k, f) = parse_line!(usize, usize);
        ks[i] = k;
        fs[i] = f;
    }
    let mut result = Vec::new();
    for q in 1..=100 {
        let mut ok = true;
        for i in 0..m {
            ok &= (ks[i] - 1) / q + 1 == fs[i];
        }
        if ok {
            result.push((n - 1) / q + 1);
        }
    }
    result.sort_unstable();
    result.dedup();
    if result.len() != 1 {
        println!("-1");
    }else{
        println!("{}", result[0]);
    }
}

fn main() {
    let tests = 1; // parse_line!(usize);
    for _ in 0..tests {
        solve();
    }
}


R=lambda:map(int,raw_input().split())
n,m=R()
l,h=0,111
for _ in range(m):
    a,b=R()
    l=max(l,(a+b-1)/b)
    if b>1:
        h=min(h,(a-1)/(b-1))
a=set((n+x-1)/x for x in range(l,h+1))
if 1==len(a):
    print a.pop()
else:
    print -1


R=lambda:map(int,raw_input().split())
n,m=R()
l,h=1,1111
for _ in range(m):
    a,b=R()
    l=max(l,(a+b-1)/b)
    if b>1:
        h=min(h,(a-1)/(b-1))
a=set((n+x-1)/x for x in range(l,h+1))
if 1==len(a):
    print a.pop()
else:
    print -1


R=lambda:map(int,raw_input().split())
n,m=R()
l,h=0,10**9
for _ in range(m):
    a,b=R()
    l=max(l,(a+b-1)/b)
    if b>1:
        h=min(h,(a-1)/(b-1))
a=set((n+x-1)/x for x in range(l,h+1))
if 1==len(a):
    print a.pop()
else:
    print -1


R=lambda:map(int,raw_input().split())
n,m=R()
l,h=0,10**9
for _ in range(m):
    a,b=R()
    l=max(l,(a+b-1)/b)
    if b>1:
        h=min(h,(a-1)/(b-1))
print n/l+1 if l==h else -1


n,m=map(int,raw_input().split())
from math import *
v=[]
for i in range(0,m):
  k,f=map(int,raw_input().split())
  v.append((k,f))
ans=set()
def val(a,b):
  if a%b==0:
    return a/b
  else:
    return a/b+1
#print v
for i in range(1,101):
  c=0
  for j in range(0,m):
    #print val(v[j][0],i),i
    if val(v[j][0],i)!=v[j][1]:

      c=1
      break
  if c==0:
    ans.add(val(n,i))
ans=list(ans)
if len(ans)!=1:
  print -1
else:
  print ans[0]


n,m=map(int,raw_input().split())
from math import *
v=[]
for i in range(0,m):
  k,f=map(int,raw_input().split())
  v.append((k,f))
ans=set()
def val(a,b):
  if a%b==0:
    return a/b
  else:
    return a/b+1
#print v
for i in range(1,101):
  c=0
  for j in range(0,m):
    #print val(v[j][0],i),i
    if val(v[j][0],i)!=v[j][1]:

      c=1
      break
  if c==0:
    ans.add(val(n,i))
ans=list(ans)
if len(ans)!=1:
  print -1
else:
  print val(n,ans[0])


n,m=map(int,raw_input().split())
from math import *
v=[]
for i in range(0,m):
  k,f=map(int,raw_input().split())
  v.append((k,f))
ans=[]
def val(a,b):
  if a%b==0:
    return a/b
  else:
    return a/b+1
#print v
for i in range(1,101):
  c=0
  for j in range(0,m):
    #print val(v[j][0],i),i
    if val(v[j][0],i)!=v[j][1]:

      c=1
      break
  if c==0:
    ans.append(val(v[j][0],i))
#print ans
if len(ans)!=1:
  print -1
else:
  print val(n,ans[0])


n,m=map(int,raw_input().split())
from math import *
v=[]
for i in range(0,m):
  k,f=map(int,raw_input().split())
  v.append((k,f))
ans=[]
def val(a,b):
  if a%b==0:
    return a/b
  else:
    return a/b+1
#print v
for i in range(1,101):
  c=0
  for j in range(0,m):
    #print val(v[j][0],i),i
    if val(v[j][0],i)!=v[j][1]:

      c=1
      break
  if c==0:
    ans.append(val(v[j][0],i))
#print ans
if len(ans)!=1:
  print -1
else:
  print ans[0]


n,m=[int(i) for i in input().split()]
a=[]
for i in range(m):
    k,l=[int(i) for i in input().split()]
    a.append([k,l])
b=set()
for x in range(1,105):
    for i in range(len(a)):
        q=(a[i][1]-1)*x+1
        e=a[i][1]*x
        if a[i][0]<q or a[i][0]>e:
            break
    else:
        b.add((n+x-1)//x)
if len(b)>1:
    print(-1)
else:
    print(*b)


n,m=[int(i) for i in input().split()]
a=[]
for i in range(m):
    k,l=[int(i) for i in input().split()]
    a.append([k,l])
b=[]
for x in range(1,105):
    for i in range(len(a)):
        q=(a[i][1]-1)*x+1
        e=a[i][1]*x
        if a[i][0]<q or a[i][0]>e:
            break
    else:
        b.append((n+x-1)//x)
if len(b)>1:
    print(-1)
else:
    print(b[0])


n, m = map(int, input().split())
kf = [tuple(map(int, input().split())) for _ in range(m)]
ans = []

for i in range(1, 101):
    for k, f in kf:
        if (k+i-1)//i!=f:
            break
    else:
        ans.append((n+i-1)//i)

if len(ans)==1:
    print(ans[0])
else:
    print(-1)


n, m = map(int, input().split())
kf = [tuple(map(int, input().split())) for _ in range(m)]
ans = set()

for i in range(1, 101):
    for k, f in kf:
        if (k+i-1)//i!=f:
            break
    else:
        ans.add((n+i-1)//i)

if len(ans)==1:
    print(list(ans)[0])
else:
    print(-1)