def main():
    n, x = map(int, input().split())

    remainder = n - (2 * x)

    if remainder == 0:
        print(3 * x)
        return

    if remainder < 0:
        n, x = x, (n - x)
    else:
        n -= x

    ans = 0
    while True:
        num = n // x
        ans += (3 * x) * num
        rem = n % x
        if rem == 0:
            break
        n, x = x, rem
    print(ans)


if __name__ == "__main__":
    main()
