#include <bits/stdc++.h>
int solve(int n, int x) {
  if (n % x == 0) return n + x;
  return solve(x, n % x) + n / x * x * 2;
}
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  printf("%d", solve(n - x, x) + n);
}
