#include <iostream>
#include <cstdio>
using namespace std;
long long n,d,a,b,h;
long long m;
int main()
{
freopen("mountain.in","r",stdin);
freopen("mountain.out","w",stdout);
cin>>n>>d>>a>>b;
if (a>b)
{
int t=a; a=b; b=t;
}
m=(b-a)/d;
n--;//减去第一分钟
n-=m;
if (n%2!=0|| n%2==0 && (b-a)%d==0)
{
h=b+n/2*d;
}
else
{
h=b+(n-1)/2*d+(d-(b-a)%d);
}
cout<<h<<endl;
return 0;
}