#include <bits/stdc++.h>
int main() {
  long long n, x, ans, xx, yy;
  std::cin >> n >> x;
  if (n % 2 == 0 and n / 2 == x) {
    ans = 3 * x;
  } else {
    if (x > n / 2) {
      x = n - x;
    }
    xx = x;
    yy = n - x;
    ans = n;
    bool f = false;
    while (1) {
      if (yy >= xx) {
        ans += 2 * xx;
        yy -= xx;
        if (yy == 0) {
          ans -= xx;
          f = true;
          break;
        }
        if (f) {
          break;
        }
      }
      if (xx > yy) {
        ans += yy * 2;
        xx -= yy;
        if (xx == 0) {
          ans -= yy;
          f = true;
          break;
        }
        if (f) {
          break;
        }
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
