#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  int ans = n;
  if (n = 2 * x) {
    cout << 3 * x << endl;
    return 0;
  }
  int a = n - x, b = x;
  if (a < b) swap(a, b);
  int tmp;
  while (a != b * 2) {
    tmp = a / b;
    ans += 2 * b * tmp;
    a -= b * tmp;
    swap(a, b);
  }
  ans += 3 * b;
  cout << ans << endl;
  return 0;
}
