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