#include <bits/stdc++.h>
using namespace std;
int square(long long int n, long long int x, long long int sum);
int hishi(long long int n, long long int x, long long int sum);
int solve(long long int n, long long int x, long long int sum) {
  if (n % 2 == 0 && x == n / 2) return 3 * x + sum;
  if (x < (double)n / 2) return solve(n - x, x, n + x + sum);
  return square(x, n - x, x + sum);
}
int square(long long int n, long long int x, long long int sum) {
  if (n % x == 0) return 2 * n - x + sum;
  return hishi(x, n % x, 2 * x * (n / x) + x + sum);
}
int hishi(long long int n, long long int x, long long int sum) {
  if (n % x == 0) return 2 * n - x + sum;
  return hishi(x, n % x, 2 * x * (n / x) + sum);
}
int main() {
  long long int n, x;
  cin >> n >> x;
  cout << solve(n, x, 0) << endl;
  return 0;
}
