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