#include <bits/stdc++.h>
using namespace std;
long long n;
long long x;
int main() {
  cin >> n >> x;
  long long result = x;
  while (true) {
    if (n - x == x) {
      result += x + x;
      cout << result << endl;
      break;
    }
    if (n - x < x) {
      long long up = x - (n - x);
      long long down = x;
      result += (n - x) << 1;
      x = up;
      n = down;
    } else {
      long long up = (n - x) - x;
      long long down = n - x;
      result += (n - x) + x;
      x = up;
      n = down;
    }
  }
}
