f::Integer->Integer->Integer->Integer->Integer
f _ _ 0 ans=ans
f _ 0 _ ans=ans
f 1 w h ans
    |h<w=f 2 w h ans
    |h `mod` w==0 =let d=(h `div` w)*w*2
                   in f 0 0 0 (ans+d-w)
    |otherwise=let d=(h `div` w)
               in f 1 w (h-d*w) (ans+d*w+(d-1)*w+w)
f 2 w h ans
   |w<h=f 1 w h ans
   |w `mod` h==0=let d=(w `div` h)*h*2
                 in f 0 0 0 (ans+d-h)
   |otherwise=let d=(w `div` h)
              in f 2 (w-d*h) h (ans+d*h+(d-1)*h+h)

main = do
       e1<-getLine
       let (n:x:[])=map read (words e1)::[Integer]
       print $ f 1 x (n-x) n