
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def main():
    hen, start=map(int,input().split())

    koyakusuu = gcd(hen, start)
    ans = koyakusuu * 3 * (hen // koyakusuu - 1)
    print(ans)

if __name__ == '__main__':
    main()