#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int x;
  cin >> x;
  int asw;
  int r = 1;
  int a;
  int b;
  if (x == n / 2 && n % 2 == 0) {
    asw = 3 * n / 2;
  } else {
    if (x < (double)n / 2) {
      a = n - x;
      b = x;
      asw = a + b;
    } else if (x > (double)n / 2) {
      a = x;
      b = n - x;
      asw = a;
    }
    int tmp;
    while (r != 0) {
      r = a % b;
      tmp = a / b;
      asw += 2 * tmp * b;
      a = b;
      b = r;
    }
    asw -= a;
  }
  cout << asw << "\n";
  return 0;
}
