#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  long long u = x, v = n - x, ans = n;
  while (u != v) {
    if (u < v) {
      ans += 2 * u;
      v = v - u;
    } else {
      ans += 2 * v;
      u = u - v;
    }
  }
  cout << ans + u;
  return 0;
}
