///
// File:  b.go
// Author: ymiyamoto
//
// Created on Sat Jun  9 16:58:59 2018
//
package main

import "fmt"

var N, X int

func f(a, b int) int {
	if a == b {
		return a
	}

	if a > b {
		return 2*b + f(a-b, b)
	}
	return 2*a + f(a, b-a)
}

func main() {
	fmt.Scan(&N, &X)

	fmt.Println(N + f(X, N-X))
}
