#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1e18 + 7;
long long a[200000];
signed main() {
  long long n, x, ans = 0;
  scanf("%lld%lld", &n, &x);
  while (1) {
    if (n % x == 0) {
      ans += 3 * x * ((n - x) / x);
      break;
    } else if (n - x > x) {
      ans += 3 * x * ((n - x) / x);
      n = n % x;
    } else {
      ans += 3 * n * (x / n);
      x = x % n;
      if (x == 0) break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
