#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}
int main(void) {
  int n, x;
  cin >> n >> x;
  int gc = gcd(n - x, x);
  cout << 3 * (n - gc) << endl;
  return 0;
}
