#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int l[201];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  ans += n;
  long long dai = max(x, n - x);
  long long syou = min(x, n - x);
  while (1) {
    int ko = dai / syou;
    dai -= ko * syou;
    ans += 2 * syou * ko;
    if (dai == 0) {
      ans -= syou;
      break;
    }
    long long tmp = dai;
    dai = syou;
    syou = dai;
  }
  cout << (ans) << endl;
  return 0;
}
