#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) return b * (2 * (a / b) - 1);
  return a / b * 2 * b + gcd(b, a % b);
}
int main() {
  long long int a, b;
  cin >> a >> b;
  cout << a + gcd(b, a - b) << endl;
}
