#include <bits/stdc++.h>
using namespace std;
int gcd(int m, int n) {
  if ((0 == m) || (0 == n)) return 0;
  if (n > m) {
    swap(m, n);
  }
  while (n > 0) {
    m = m % n;
    swap(m, n);
  }
  return m;
}
int main() {
  int N, X;
  cin >> N >> X;
  int n;
  n = gcd(N, X);
  cout << (3 * (N / n) - 3) * n << endl;
  return 0;
}
