def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

def solve(n,x):
    res = 3 * (n - gcd(n,x))
    return res



if __name__ == '__main__':
    n,x = map(int,input().split())
    print(solve(n,x))