local mmi, mma = math.min, math.max local mfl, mce = math.floor, math.ceil local t = io.read("*n") local x, a = io.read("*n", "*n") local y, b = io.read("*n", "*n") local low = -1 local cand = -1 if 0 < t then low = mfl(t / a) cand = (t - a * low) + x * low else low = -1 cand = 1000000007*1000000007 end for i = 1, 10 * 1000 * 1000 do local ecceed = a * (low + i) - t back_cnt = mce(ecceed / b) rem = b * back_cnt - ecceed cand = mmi(cand, rem + (i + low) * x + back_cnt * y) end cand = tostring(1LL * cand):gsub("LL", "") print(cand)