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