#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
  long long x, n;
  cin >> n >> x;
  long long t = gcd(x, n);
  long long u = n / t, v = 3 * u - 3;
  cout << n * v / u;
  return 0;
}
