{*************************************************************************
 *                                                                       *
 *                       Pogromcy Algorytmów 2004                        * 
 *                                                                       *
 *   Rozwi±zanie zadania: Tomki                                          *
 *   Plik:                tom.pas                                        *
 *   Autor:               Marcin Mucha                                   *
 *************************************************************************}

{Program wzorcowy do zadania "Tomki" (dawniej Skoczki 2)}

type
   vector = record x, y	: longint end;

const
   BIGNUMBER : longint = 1000000;

var
   f	      : text;
   a, b, best : vector;

function abs(x : longint) : longint;
begin
   if x >= 0 then abs := x else abs := -x
end; { abs }

function abs_real(x : real) : real;
begin
   if x >= 0 then abs_real := x else abs_real := -x
end; { abs_real }

function norm(var v : vector) : longint;
begin
   norm := abs(v.x)+abs(v.y)
end; { norm }

procedure xch(var a, b : vector);
var
   c : vector;
begin
   c := a; a := b; b := c
end;

procedure find_best(a, b : vector; var best : vector);
{ To nie jest zakodowane superefektywnie }
{ Dziala to na korzysc zawodnikow :)     }
var
   r		: longint;
   r1, r2	: longint;
   norm1, norm2	: longint;
   rx, ry	: real;
   normx, normy	: real;

begin
   if (a.x=0) and (a.y=0) then
      best := b
   else if (b.x=0) and (b.y=0) then
      best := a
   else
   begin
      if norm(b) > norm(a) then xch(a,b);
      repeat { norm(b) <= norm(a) }
	 if (b.x=0) then
	    r := round(a.y/b.y)
	 else if (b.y=0) then
	    r := round(a.x/b.x)
	 else
	 { Ten kawalek zalezy od normy jakiej sie uzywa }
	 begin
	    { Przeciecia z osiami x, y i normy tych przeciec }
	    rx := a.x/b.x;
	    ry := a.y/b.y;
	    normx := abs_real(a.y-rx*b.y);
	    normy := abs_real(a.x-ry*b.x);
	    if normx < normy then
	    begin
	       r1 := trunc(rx);
	       r2 := BIGNUMBER-trunc(BIGNUMBER-rx)
	    end
	    else
	    begin
	       r1 := trunc(ry);
	       r2 := BIGNUMBER-trunc(BIGNUMBER-ry)
	    end;
	    norm1 := abs(a.x-r1*b.x)+abs(a.y-r1*b.y);
	    norm2 := abs(a.x-r2*b.x)+abs(a.y-r2*b.y);
	    if norm1 < norm2 then r := r1 else r := r2
	 end;
	 { Koniec tego kawalka }
	 a.x := a.x-r*b.x;
	 a.y := a.y-r*b.y;
	 xch(a,b);
      until (norm(b) >= norm(a)) or (norm(b)=0); { w zasadzie mog± być liniowo zależne :) }
      best := a
   end;
end; { find_best }
			 
begin
   assign(f,'sko.in');
   reset(f);
   readln(f,a.x,a.y,b.x,b.y);
   close(f);
   
   find_best(a, b, best);
   
   assign(f,'sko.out');
   rewrite(f);
   writeln(f,norm(best));
   close(f)
end.
