#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, x;
  cin >> n >> x;
  int len = 0;
  len += x + (n - x);
  int a, b;
  a = x;
  b = n - x;
  if (a > b) swap(a, b);
  while (true) {
    if (b % a == 0) {
      len += (2 * b / a - 1) * a;
      break;
    } else {
      len += 2 * (b / a) * a;
    }
    int tmp = b;
    b = a;
    a = tmp % a;
  }
  cout << len << "\n";
  return 0;
}
