#include <bits/stdc++.h>
using namespace std;
int n, x;
int gcd(int &x, int &y) {
  int ans = 1;
  for (int i = min(x, y); i > 0; i--) {
    if (x % i == 0 && y % i == 0) {
      ans = i;
      break;
    }
  }
  return ans;
}
int main() {
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
  return 0;
}
