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