program cojs_p486;
var
l,i,j,c0,c1,m0,m1,l0,l1:longint;
procedure ass;
begin
assign(input,'bs.in');
assign(output,'bs.out');
reset(input);
rewrite(output);
end;
procedure cls;
begin
close(input);
close(output);
end;
procedure swap(var a,b:longint);
var
t:longint;
begin
t:=a;
a:=b;
b:=t;
end;
begin
ass;
while not(eof) do
begin
readln(c0,c1,m0,m1);
if m0<>0 then
l0:=c0 div m0;
if m1<>0 then
l1:=c1 div m1;
if l0>l1
then begin swap(c0,c1); swap(m0,m1); swap(l0,l1); end;
if (m0=0) and (m1<>0)
then begin l:=m1; writeln(l); end;
if (m1=0) and (m0<>0)
then begin l:=m0; writeln(l); end;
if (m1=0) and (m0=0)
then writeln('0');
if (m1<>0) and (m0<>0)
then begin l:=c0+(l0+1)*m1; writeln(l); end;
end;
cls;
end.