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