def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, X = ZZ()
    N -= X
    ans = 0
    while True:
        ans += (N//X) * 3 * X
        #割り切れない
        if N%X: N, X = X, N%X
        #割り切れる
        else: break
    print(ans)

    return

if __name__ == '__main__':
    main()
