program main
  implicit none
  integer(8)n,x,s
  read*,n,x
  s=x
  do
     n=n+x
     x=n-x
     n=n-x
     if ( n .lt. x ) then
	x=x-n
     end if
     s=s+x*(n/x)+x

     if ( x .eq. 1 .or. mod(n,x) .eq. 0 )  then
	exit
     end if
  end do
  print'(i0)',s
end program main