#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n, x;
  cin >> n >> x;
  long long ans = x + n - x;
  long long width = n - (n - x);
  long long height = n - x;
  while (true) {
    if (width >= height) {
      ans += (width / height) * height * 2;
      if (width % height == 0) {
        ans -= height;
        break;
      }
      width = width % height;
    } else if (width < height) {
      ans += (height / width) * width * 2;
      if (width % height == 0) {
        ans -= width;
        break;
      }
      height = height % width;
    }
  }
  cout << ans << endl;
}
