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