#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, len, tra;
  cin >> n >> x;
  if (x > n / 2) x = n - x;
  len = n - x;
  tra = n;
  while (len % x != 0) {
    tra += (len / x * 2) * x;
    int me = len;
    len = x;
    x = me % x;
  }
  if (len) tra += (len / x * 2 - 1) * x;
  cout << tra << endl;
  return 0;
}
