#include using namespace std; using ll = long long; using ld = long double; using pll = pair; #define rep(i,n) for (int i = 0; i < (n); i++) #define rep1(i,n) for (int i = 1; i <= (n); i++) #define all(a) begin(a), end(a) // aよりもbが大きいならばaをbで更新する // (更新されたならばtrueを返す) template bool chmax(T &a, const T& b) { if (a < b) { a = b; // aをbで更新 return true; } return false; } // aよりもbが小さいならばaをbで更新する // (更新されたならばtrueを返す) template bool chmin(T &a, const T& b) { if (a > b) { a = b; // aをbで更新 return true; } return false; } ll modpow(ll a, ll b, ll m) { ll p = a; ll ans = 1; rep(i,32) { if ((b & (0x01 << i)) != 0) { ans *= p; ans %= m; } p *= p; p %= m; } return (ans); } ll pow(ll a, ll b) { ll p = a; ll ans = 1; rep(i,32) { if ((b & (0x01 << i)) != 0) { ans *= p; } p *= p; } return (ans); } ll Division(ll a, ll b, ll m) { return ((a * modpow(b, m - 2, m)) % m); } struct V { int x, y; V(int x=0, int y=0): x(x), y(y) {} V operator-(const V &a) const { return V(x-a.x, y-a.y); } int cross(const V &a) const { return x*a.y - y*a.x; } int ccw(const V &a) const { int area = cross(a); if (area > 0) return +1; // ccw if (area < 0) return -1; // cw return 0; // collinear } }; /* 2147483648 int max 1000000000 1e9 9223372036854775807 ll max 1000000000000000000 1e18 */ int main() { ll t; cin >> t; ll x, a, y, b; cin >> x >> a >> y >> b; if (t > 0) { ll now = 0; ll c0 = (t-now)/a*x; c0 += t%a; now = 0; ll c1 = ((t+a-1)/a)*x; now = (t+a-1)/a*a; c1 += (now-t+b-1)/b*y; now -= (now-t+b-1)/b*b; c1 += (t-now); now = 0; ll c2 = (t+a+b-1)/a*x; now = (t+a+b-1)/a*a; c2 += (now-t+b-1)/b*y; now -= (now-t+b-1)/b*b; c2 += (t-now)/a*x; now += (t-now)/a*a; c2 += t-now; chmin(c0,c1); chmin(c0,c2); cout << c0 << endl; } else { t = -t; ll now = 0; ll c0 = (t-now+b-1)/b*y; now = (t-now+b-1)/b*b; c0 += (now-t)/a*x; now -= (now-t)/a*a; c0 += abs(t-now); now = 0; ll c1 = (t-now+a+b-1)/b*y; now = (t-now+a+b-1)/b*b; c1 += (now-t)/a*x; now -= (now-t)/a*a; c1 += (now-t); cout << min(c0,c1) << endl; } return 0; }