#include <bits/stdc++.h>
using namespace std;
long long solve(long long n, long long x) {
  if (x + x == n) return x * 3;
  if (x + x < n) {
    long long ans = x + x + n;
    long long tmp = n - x - x;
    if (x % tmp == 0) return ans + ((x / tmp) * 2 - 1) * tmp;
    return ans + ((x / tmp) * 2 - 1) * tmp + solve(x + x % tmp, x % tmp) -
           x % tmp;
  }
  long long ans = x;
  long long tmp = x + x - n;
  if (x % tmp == 0) return ans + ((x / tmp) * 2) * tmp;
  return ans + ((x / tmp) * 2) * tmp + solve(x + x % tmp, x % tmp) - x % tmp;
}
int main() {
  long long n, x;
  cin >> n >> x;
  cout << solve(n, x);
}
