#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int inf = 1 << 30;
const double pi = acos(-1);
const double eps = 1e-8;
long long n, x, ans, h, w;
int main() {
  cin >> n >> x;
  ans = n;
  h = n - x;
  w = x;
  while (1) {
    if (h > w) {
      if (h % w == 0) {
        ans += (2 * (h / w) - 1) * w;
        break;
      }
      ans += (2 * h / w) * w;
      h %= w;
    } else if (h < w) {
      if (w % h == 0) {
        ans += (2 * (w / h) - 1) * h;
        break;
      }
      ans += (2 * w / h) * h;
      w %= h;
    } else {
      ans += h;
      break;
    }
  }
  cout << ans << endl;
}
