#include <bits/stdc++.h>
using namespace std;
long long n, x, ans;
void solve(long long nn, long long xx) {
  xx = xx < (nn - xx) ? xx : (nn - xx);
  if (nn % xx == 0) {
    ans += (nn / xx - 1) * xx * 3;
    printf("%lld\n", ans);
  } else {
    ans += (nn / xx - 1) * xx * 3;
    solve(nn - xx, nn % xx);
  }
}
int main() {
  cin >> n >> x;
  x = x < (n - x) ? x : (n - x);
  if (n % x == 0) {
    ans = (n / x - 1) * 3 * x;
    printf("%lld\n", ans);
  } else {
    ans = (n / x - 1) * x * 3;
    solve(n - x, n % x);
  }
  return 0;
}
