#include <bits/stdc++.h>
using namespace std;
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;
  int ga = gcd(n, x);
  cout << (n - ga) * 3 << endl;
  return 0;
}
