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)  then
        exit
     else
        if ( mod(n,x) .eq. 0 ) then
           s=s-x
        endif
     end if
  end do
  print'(i0)',s
end program main
