#include <bits/stdc++.h>
using namespace std;
long long x, y;
long long n, a;
long long ans = 0;
int main() {
  cin >> n >> a;
  ans += a;
  ans += (n - a);
  x = a;
  y = n - a;
  while (x != y) {
    if (x < y) {
      ans += 2 * x;
      y -= x;
    } else {
      ans += 2 * y;
      x -= y;
    }
  }
  ans += x;
  cout << ans << endl;
  return 0;
}
