let l = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"); let n = BigInt(l[0]); let [a1, b1] = l[1].split(" ").map(s => BigInt(s)); let [a2, b2] = l[2].split(" ").map(s => BigInt(s)); let [a3, b3] = l[3].split(" ").map(s => BigInt(s)); if (a2 * b1 < a1 * b2) { [a1, b1, a2, b2] = [a2, b2, a1, b1]; } if (a3 * b1 < a1 * b3) { [a1, b1, a3, b3] = [a3, b3, a1, b1]; } let r = 0n, a1b2 = a1 * b2, a1b3 = a1 * b3; for (let p = 0n, s = n; p < a1b2 && s >= 0n; p += b2, s -= a2) { for (let q = 0n, t = s; q < a1b3 && t >= 0n; q += b3, t -= a3) { let u = t / a1 * b1 + p + q; r = (r < u) ? u : r; } } console.log(r.toString());