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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

int dp[3000000];

int main(){
  int n;cin>>n;
  vector<int> v(n);
  REP(i,n)cin>>v[i];
  int sum=0;
  REP(i,n)sum+=v[i];
  dp[0]=1;
  REP(i,n){
    for(int j=sum;j>=0;j--){
      if(dp[j])dp[j+v[i]]=1;
    }
  }
  int ans=1e9;
  for(int i=0;i<=sum;i++)if(dp[i])ans=min(ans,max(sum-i,i));
  cout<<ans<<endl;
}

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

typedef long long int ll;
constexpr int kN = int(1E5 + 10);

int a[kN], b[kN], y[kN], x[kN];

int main() {
	int n, l, idx = -1, now = 0, lst = 0;
	ll ans = 0;
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
	a[0] = b[0] = 0, a[n + 1] = b[n + 1] = l + 1;
	for (int i = 0; i <= n; i++) x[i] = a[i + 1] - a[i] - 1;
	for (int i = 0; i <= n; i++) y[i] = b[i + 1] - b[i] - 1;
	for (int i = 0; i <= n; i++) if (y[i]) {
		now = 0;
		while (x[lst] == 0) lst++;
		while (now < y[i]) now += x[++idx];
		if (now > y[i]) {
			printf("-1\n");
			return 0;
		}
		ans += max(i - lst, 0) + max(idx - i, 0);
		lst = idx + 1;
	}
	printf("%lld\n", ans);
}
