#include<iostream>
#include<cstdio>
using namespace std;
long long a,b,n,d,pr;
inline bool g(long long x)
{
long long x1=(x-a)/d;
if(x1*d<x-a)
++x1;
long long x2=(x-b)/d;
if(x2*d<x-b)
++x2;
if(x1+x2<n)
{
pr=x;
return 1;
}
return 0;
}
int main()
{
freopen("mountain.in","r",stdin);
freopen("mountain.out","w",stdout);
scanf("%lld%lld%lld%lld",&n,&d,&a,&b);
long long z=b,y=a+n*d;
while(z<=y)
{
long long mid=(z+y)>>1;
if(g(mid))
z=mid+1;
else
y=mid-1;
}
printf("%lld",pr);
/*getchar();
getchar();*/
}