#include <bits/stdc++.h>
using namespace std;
int main() {
  long long numa, x;
  cin >> numa >> x;
  if (x <= numa / 2) {
    x = numa - x;
  }
  long long numb = numa;
  long long ans = 0;
  for (;;) {
    ans += ((numa / x) * 2 - 1) * x;
    numa %= x;
    if (numa < numb) {
      swap(numa, numb);
    }
    x = numb;
    if (x == 0) {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
