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