#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, res = 0, ans = 0;
  scanf("%d %d", &n, &x);
  if (x < n - x) {
    x = n - x;
  }
  do {
    res = n - x;
    ans += res * 3 * (x / res);
    n = x;
    x = res;
  } while (n % x != 0);
  printf("%d\n", ans);
  return 0;
}
