#include <bits/stdc++.h>
#define LL long long
using namespace std ;
LL gcd ( LL a, LL b ) { return b?gcd(b,a%b):a ; }
int main() {
	LL x, n ;
	scanf ( "%lld%lld", &n, &x ) ;	
	printf ( "%lld\n", 3*(n-gcd(n,x)) ) ;
	return 0 ;
}