#if !__INCLUDE_LEVEL__ #include __FILE__ signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll T, X, A, Y, B; cin >> T >> X >> A >> Y >> B; bool is_back = T < 0; if (T < 0) { T *= -1; swap(X, Y); swap(A, B); } // B *= -1; if ((!is_back) && X >= A) { cout << T << "\n"; return 0; } ll TT = T % A; ll ans = T / A * X; cerr << ans << "\n"; ll add_cost = is_back ? 1e14 : TT; REP(i, 1e7) { if (i == 0) continue; ll cost = X * i; if ((A * i - TT) % B == 0) { cost += Y * (A * i - TT) / B; } else { cost += Y * ((A * i - TT) / B) + ((A * i - TT) % B); } add_cost = min(add_cost, cost); } cout << ans + add_cost << "\n"; return 0; } //====================temp==================== #else #include using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define RREP(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define REPITR(itr, ARRAY) \ for (auto itr = (ARRAY).begin(); itr != (ARRAY).end(); ++itr) #define RREPITR(itr, ARRAY) \ for (auto itr = (ARRAY).rbegin(); itr != (ARRAY).rend(); ++itr) #define ALL(n) (n).begin(), (n).end() using ll = long long; using ull = unsigned long long; //#define int long long template struct edge { int to; T cost; edge() {} edge(int to, T cost): to(to), cost(cost) {} }; #endif