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