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