#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) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int main() {
  long long n, x;
  scanf("%I64d%I64d", &n, &x);
  long long ans = 0;
  long long g = gcd(n, x);
  n /= g;
  ans = (n - 1) * 3 * g;
  printf("%I64d\n", ans);
}
