#include <cstdio>
#include <algorithm>
using namespace std;
long long n,d,a,b;
long long co(long long s,long long t){
if(s>t)return co(t,s);
if((t-s)%d==0)return (t-s)/d;
else return (t-s)/d+1;
}
bool is_okay(long long h){
if(co(a,h)+co(h,b)<n)return true;
else return false;
}
int main(){
long long l,r,mid;
freopen("mountain.in","r",stdin);
freopen("mountain.out","w",stdout);
scanf("%lld%lld%lld%lld",&n,&d,&a,&b);
l=max(a,b);
r=l+n*d;
while(r!=l){
mid=(l+r)/2;
if(is_okay(mid))l=mid+1;
else r=mid;
}
printf("%lld\n",l-1);
return 0;
}