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