#include <bits/stdc++.h>
using namespace std;
long long modexp(long long base, long long exp) {
  if (exp == 1)
    return base;
  else if (exp == 0)
    return 1;
  else {
    if (exp % 2 == 0) {
      long long base1 = pow(modexp(base, exp / 2), 2);
      if (base1 >= 1000000007)
        return base1 % 1000000007;
      else
        return base1;
    } else {
      long long ans = (base * pow(modexp(base, (exp - 1) / 2), 2));
      if (ans >= 1000000007)
        return ans % 1000000007;
      else
        return ans;
    }
  }
}
long long gcd(long long a, long long b) {
  if (!a) return b;
  return gcd(b % a, a);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, x;
  cin >> n >> x;
  int d = gcd(n, x);
  cout << 3 * (n - d);
}
