#include <bits/stdc++.h>
using namespace std;
long long n, x, sum, len, t, temp;
int main() {
  scanf("%lld%lld", &n, &x);
  if (x > n - x) x = n - x;
  if (n % x == 0)
    printf("%lld\n", 2 * x * (n / x - 1) + n - x);
  else if (x % (n % x) == 0)
    printf("%lld\n",
           2 * (n / x) * x - x + x / (n % x) * (n % x) * 2 + n - n % x - x);
  else {
    len = n - x;
    t = x;
    sum = n;
    while (len % t != 0) {
      sum = sum + 2 * (len / t) * t;
      temp = t;
      t = len % t;
      len = temp;
    }
    sum = sum + (len / t) * t + t;
    printf("%lld\n", sum);
  }
}
