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