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