#include <bits/stdc++.h>
int main() {
  int n, x, sum = 0, temp;
  scanf("%d%d", &n, &x);
  n -= x;
  while (x) {
    sum += n - n % x;
    temp = n;
    n = x;
    x = temp % x;
  }
  printf("%d\n", sum * 3);
  return 0;
}
