#include <bits/stdc++.h>
using namespace std;
int solve(int a, int b) {
  if (a == 0 || b == 0) return 0;
  if (a > b) return 3 * b + solve(a - b, b);
  return 3 * a + solve(b - a, a);
}
int main() {
  int N, X;
  scanf("%d %d", &N, &X);
  printf("%d\n", solve(N - X, X));
}
