#include <bits/stdc++.h>
using namespace std;
int calc(int width, int height) {
  if (height > width) {
    const int q = height / width;
    const int r = height % width;
    if (r == 0)
      return 2 * q * width - width;
    else
      return 2 * q * width + calc(width, r);
  } else if (height < width) {
    const int q = width / height;
    const int r = width % height;
    if (r == 0)
      return 2 * q * height - height;
    else
      return 2 * q * height + calc(r, height);
  } else {
    return height;
  }
}
int main(int argc, char *argv[]) {
  int ans = 0;
  int N, X;
  cin >> N >> X;
  ans = calc(X, N - X);
  cout << ans + N << endl;
  return 0;
}
