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