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