#include <bits/stdc++.h>
using namespace std;
long long int sum = 0;
long long int pre = 0;
int bol = 1;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  if (b == 1) {
    pre = a;
  }
  bol = bol * -1;
  return gcd(b, a % b);
}
int main() {
  long long int n, x;
  std::cin >> n >> x;
  long long int num = 0;
  long long int g = gcd(n, x);
  if (n % x != 0) {
    if (bol == 1) {
      num = (pre - 1) * g;
    } else {
      num = pre;
    }
  }
  sum = std::max(n - x, x);
  std::cout << num * 3 + sum * 3 << std::endl;
  return 0;
}
