#include #define show(x) std::cerr << #x << " = " << x << std::endl using ll = long long; int main() { int N; ll M; std::cin >> N >> M; std::vector A(N); std::vector K(N); for (int i = 0; i < N; i++) { std::cin >> A[i]; } int max = 0; for (int i = 0; i < N; i++) { std::cin >> K[i], M -= K[i] * A[i], max = std::max(max, A[i]); } ll ans = std::max(0LL, M - max * max) / max; M = ans * max - M; std::vector memo(max * max, max); auto dp = [&](auto&& self, const ll M) -> int { if (M == 0) { return 0; } if (M < 0) { return max; } if (memo[M] != max) { return memo[M]; } int ans = max; for (const ll a : A) { ans = std::min(ans, self(self, M - a)); } return memo[M] = ans + 1; }; const int add = dp(dp, M); if (add >= max) { return std::cout << -1 << std::endl, 0; } std::cout << ans + add << std::endl; return 0; }