#include <bits/stdc++.h>
int main() {
  std::uint64_t SizeOfTriangle;
  std::uint64_t InputMysteriousLight;
  std::cin >> SizeOfTriangle >> InputMysteriousLight;
  std::uint64_t LocusLength = SizeOfTriangle;
  std::uint64_t a = InputMysteriousLight;
  std::uint64_t b = (SizeOfTriangle - InputMysteriousLight);
  std::uint64_t l = (a > b) ? a : b;
  std::uint64_t s = (a < b) ? a : b;
  while (true) {
    if (l == s) {
      LocusLength += s;
      break;
    }
    if (s == 1) {
      LocusLength += l * 2 - 1;
      break;
    }
    if (s == 0) {
      break;
    }
    std::uint64_t m = l / s;
    LocusLength += (2 * s * m);
    std::uint64_t t = l % s;
    l = s;
    s = t;
  }
  std::cout << LocusLength << std::endl;
  return 0;
}
