import fractions def read_data(): N, M = map(int, input().split()) As = list(map(int, input().split())) Ks = list(map(int, input().split())) return N, M, As, Ks def solve(N, M, As, Ks): total = sum(a * k for a, k in zip(As, Ks)) if total < M: return -1 T = total - M if T == 0: return 0 if T < As[0]: return -1 if N == 1: if T % As[0]: return -1 else: return T // As[0] limits = set_limits(As) end = min(T, sum((limit - 1) * a for limit, a in zip(limits, As[:-1]))) dp = do_dp(As, limits, end) ans = float('inf') A = As[-1] r = T % A for idx in range(r, end + 1, A): coins= (T - idx) // A + dp[idx] if ans > coins: ans = coins if ans == float('inf'): ans = -1 return ans def set_limits(As): n = len(As) limits = [float('inf')] * n for i in range(n - 1): ai = As[i] for j in range(i + 1, n): aj = As[j] gcd = fractions.gcd(ai, aj) limit_i = aj // gcd limits[i] = min(limits[i], limit_i) return limits def do_dp(As, limits, end): INF = float('inf') dp = [INF] * (end + 1) dp[0] = 0 last = 0 for a, limit in zip(As[:-1], limits[:-1]): last += a * (limit - 1) dp[a] = 1 for i in range(min(last, end) - a + 1): if dp[i] != INF: j = i + a dp[j] = min(dp[j], dp[i] + 1) return dp N, M, As, Ks = read_data() ans = solve(N, M, As, Ks) print(solve(N, M, As, Ks))