#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (a != 0 && b == 0) {
    return a;
  }
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int main() {
  long long x, n;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
}
