#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, i, ans = 0, a[10000000];
  cin >> n >> x;
  a[1] = x;
  a[2] = n - x;
  if (a[1] < a[2]) {
    a[3] = x;
    ans += (a[1] + a[2]);
    i = 3;
    while (a[i - 1] % a[i] != 0) {
      a[i + 1] = a[i - 1] % a[i];
      ans += ((a[i - 1] / a[i]) * a[i] * 2);
      i++;
    }
    ans += (((a[i - 1] / a[i]) * 2 - 1) * a[i]);
  } else if (a[1] > a[2]) {
    ans += a[1];
    i = 2;
    while (a[i - 1] % a[i] != 0) {
      a[i + 1] = a[i - 1] % a[i];
      ans += ((a[i - 1] / a[i]) * a[i] * 2);
      i++;
    }
    if (i != 2)
      ans += (((a[i - 1] / a[i]) * 2 - 1) * a[i]);
    else
      ans += (((a[i - 1] / a[i]) * 2) * a[i]);
  } else {
    ans = x * 3;
  }
  cout << ans;
  return 0;
}
