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