#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X;
  cin >> N >> X;
  long long width = X, height = N - X;
  long long res = height;
  long long last_height = 0;
  while (width > 0) {
    last_height = height;
    res += width;
    long long iter = (width - 1) / height;
    res += height * iter;
    res += width;
    if (1) {
      long long new_width = width % height;
      height = height - new_width;
      width = new_width;
    } else {
      long long new_width = width % height;
      height = new_width;
      width = new_width;
    }
  }
  cout << res << endl;
}
