#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
freopen("bs.in","r",stdin);
freopen("bs.out","w",stdout);
int maxo,maxx;
int counto,countx;
while(cin>>counto>>countx>>maxo>>maxx)
{
if (counto>countx)
{
swap(counto,countx);
swap(maxo,maxx);
}
//special
if (maxo==0&&maxx==0)
{
cout<<0<<endl;
continue;
}
if (maxo==0||maxx==0)
{
cout<<min(maxo,counto)+min(maxx,countx);
continue;
}
//usual
if (counto==countx)
{
printf("%d\n",counto+countx);
continue;
}
int groupo=counto,groupx=groupo+1;
int eacho=counto/groupo,eachx=countx/groupx;
int lefto=counto-eacho*groupo,leftx=groupx-eacho*groupx;
int cano=(maxo-eacho)*groupo,canx=(maxx-eachx)*groupx;
int re=eacho*groupo+eachx*groupx+
min(cano,lefto)+min(canx,leftx);
cout<<re<<endl;
}
return 0;
}