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