#!/usr/bin/python3
import sys

sys.setrecursionlimit(100000)

N, X = list(map(int, input().split()))

def f(a,b):
    if a > b:
        a,b = b,a

    if b%a is 0:
        return int(a*(2*(b/a)-1))

    return int(2*a*int(b/a) + f(a,b%a))

print(N+f(N-X, X))
