#include <bits/stdc++.h>
using namespace std;
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
int main() {
  ios::sync_with_stdio(false);
  long long n, x, ans = 0;
  scanf("%lld%lld", &n, &x);
  ans += n;
  while (1) {
    x = min(x, n - x);
    n = max(x, n - x);
    if (n - x == 0) break;
    if (x == 1 && n == 1) break;
    ans += 2 * x;
  }
  ans += x;
  printf("%lld\n", ans);
  return 0;
}
