#include <bits/stdc++.h>
int faster_in() {
  int r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}
using namespace std;
const int INF = int(1e9 + 7);
const double PI = acos(-1);
const double EPS = 1e-9;
long long minn = 1e13;
long long get(long long a, long long b) {
  if (a == 0 || b == 0) return -minn;
  if (a < b) return get(b, a);
  minn = min(minn, a);
  minn = min(minn, b);
  return long long(a / b) * b * 2 + get(a - long long(a / b) * b, b);
}
int main() {
  long long n, x;
  cin >> n >> x;
  if (x >= (n + 1) / 2) {
    cout << get(n - x, x) + x << endl;
  } else {
    cout << get(n - x, x) + n << endl;
  }
}
