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