#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, X;
  scanf("%d %d", &N, &X);
  int ans = N;
  int l_side = N - X;
  int s_side = X;
  while (s_side > 0) {
    int num = l_side / s_side;
    int tmp_s_side = s_side;
    ans += (2 * num * s_side);
    s_side = l_side % s_side;
    l_side = tmp_s_side;
    if (s_side == 0) ans -= l_side;
  }
  printf("%d\n", ans);
  return 0;
}
