#include <bits/stdc++.h>
using namespace std;
long long int solve(long long int N, long long int X) {
  long long int p, q, sum;
  p = (N - X) / X;
  q = (N - X) % X;
  if (q == 0) {
    sum = 3 * p * X;
  } else if (X % q == 0) {
    long long int r;
    r = X / q;
    sum = 3 * p * X + 3 * q * r;
  } else {
    if (N - X >= X)
      sum = 2 * q + 3 * p * X + solve(X, X - q);
    else {
      long long int r;
      q = N - X;
      r = X / q;
      p = X - r * q;
      sum = 3 * q * r + solve(q + p, p);
    }
  }
  return sum;
}
int main() {
  long long int N, X;
  cin >> N >> X;
  long long int ans;
  ans = solve(N, X);
  cout << ans << endl;
  return 0;
}
