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