#include <bits/stdc++.h>
using namespace std;
long long int p = 0;
int cnt = 0;
long long int solve(long long int x, long long int rest) {
  if (rest == 0) return x;
  p += (x / rest) * 3 * min(x, rest);
  if (x % rest == 0) return p;
  return solve(rest, max(rest, x) - min(rest, x));
}
long long int n, x;
int main() {
  cin >> n >> x;
  long long int rest = n - x;
  if (x < rest) swap(x, rest);
  solve(x, rest);
  cout << p << endl;
  return 0;
}
