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