def knapsack_limited(vlst,wlst,mlst,W): from collections import deque INF = 1<<60 V = sum(vi*mi for vi,mi in zip(vlst,mlst)) dp = [INF]*(V+1) dp[0] = 0 Vnow = 0 for vi,wi,mi in zip(vlst,wlst,mlst): Vnow += vi*mi for i in range(vi): q1 = deque() #idx q2 = deque() #val for j in range(V+1): x = vi*j + i if x > Vnow: break vx = dp[x] - j*wi while q2 and q2[-1] > vx: q1.pop(); q2.pop() q1.append(j) q2.append(vx) dp[x] = q2[0] + j*wi if j - q1[0] == mi: q1.popleft(); q2.popleft() return dp n,m = map(int,input().split()) *a, = map(int,input().split()) *k, = map(int,input().split()) num = [0]*(n-1) for i in range(n-1): num[i] = a[i+1]-1 dp = knapsack_limited(a[:n-1],[1]*(n-1),num,0) #print(dp) an,kn = a[-1],k[-1] tot = sum(ai*ki for ai,ki in zip(a,k)) - an*kn ans = INF = 1<<60 for i,x in enumerate(dp): if x==INF: continue c = tot-i-m if c%an==0 and -c <= an*kn: #print(c,i,x,c//an) ans = min(ans,x + kn + c//an) print(ans if ans < INF else -1)