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