#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, l;
  cin >> l >> a;
  long long cnt = a, w = a, h = l - a;
  bool isY = true;
  while (w && h) {
    if (isY) {
      cnt += ((w / h) * 2 + 1) * h;
      w -= (w / h) * h;
      if (w == 0) cnt -= 2 * h;
    }
    if (!isY) {
      cnt += ((h / w) * 2) * w;
      h -= (h / w) * w;
      if (h == 0) cnt -= w;
    }
    isY = !isY;
  }
  cout << cnt << endl;
}
