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