#include <bits/stdc++.h>
int gcd(int a, int b) {
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
int main() {
  int n, x;
  int i, j, g;
  scanf("%d%d", &n, &x);
  g = gcd(n, x);
  n /= g;
  x /= g;
  printf("%d\n", (n - 1) * 3 * g);
  return 0;
}
