#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102c000000,102c000000")
using namespace std;
const int N = 3 * 1e6 + 10;
const int MOD = 1e9 + 7;
double const pi = acos(-1);
void fre() { freopen("in.txt", "r", stdin); }
long long Gcd(long long a, long long b) { return b ? Gcd(b, a % b) : a; }
int main() {
  long long n, x;
  cin >> n >> x;
  long long d = Gcd(n, x);
  n /= d;
  printf("%I64d\n", d * 3 * (n - 1));
}
