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