#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  while (b > 0) {
    int r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
}
