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