#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, X;
  cin >> N >> X;
  bool dai = true;
  long x = X, y = N - X;
  long ans = x;
  while (x > 0 && y > 0) {
    if (dai) {
      ans += y;
      dai = false;
    } else {
      long m = min(x, y), M = max(x, y);
      ans += m;
      dai = true;
      x = M - m;
      y = m;
    }
  }
  cout << ans << endl;
}
