#include <bits/stdc++.h>
using namespace std;
signed main() {
  long n, x;
  cin >> n >> x;
  long a = x, b = n - x, c = n;
  long res = x;
  if (a == b) return cout << x * 3, 0;
  while (true) {
    if (a > b) {
      res += b * 2;
      a -= b;
      c -= b;
    } else if (a < b) {
      res += c;
      long an, bn, cn;
      an = 2 * b - c;
      bn = c - b;
      cn = b;
      a = an;
      b = bn;
      c = cn;
    } else {
      res += b * 2;
      return cout << res, 0;
    }
  }
}
