#include <bits/stdc++.h>
using namespace std;
int n, x, y, ans;
inline int mx(int a, int b) { return a > b ? a : b; }
int main() {
  scanf("%d%d", &n, &x);
  y = mx(x, n - x);
  x = n - y;
  ans = n;
  while (x >= 1) {
    if (y % x == 0) {
      ans += (y / x * 2 * x - x);
      break;
    }
    ans += y / x * 2 * x;
    int tt = x;
    x = y % x;
    y = tt;
  }
  printf("%d", ans);
  return 0;
}
