import math t=int(input()) x,a=list(map(int,input().split())) y,b=list(map(int,input().split())) def f(time): if time>0: n=time%a for i in range(n//x): m=n+i*x n=min(n,(m//b)*y+m%b) print(n+(time//a)*x) elif t<0: u=abs(time) r=(math.ceil(u/b)) return(r*y+f(r*b-u)) else: return(0) print(f(t))