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