#include <bits/stdc++.h>
int main(void) {
  int n, x, a, b, ans = 0, t;
  scanf("%d%d", &n, &x);
  a = (x * 2 < n) ? x : n - x;
  b = n - a;
  while (a) {
    t = b % a;
    ans += b - t;
    b = a;
    a = t;
  }
  printf("%d", ans * 3);
  return 0;
}
