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