#include <bits/stdc++.h>
using namespace std;
double n, m, a = 0, t = 0;
vector<double> v, ans;
double keisan(double wra, double wru) {
  if (t)
    t = 0;
  else
    t = 1;
  if (wra == 0) {
    return (-1) * (wru);
  } else if (wra < wru) {
    return keisan(wru, (wru - wra)) - wru;
  } else {
    return keisan((wra - wru), wru) + wru * 2;
  }
}
signed main() {
  cin >> n >> m;
  t = 1;
  if (n / 2 >= m) {
    a = keisan((n - m), m);
    a += m + (n - m);
  } else {
    t = 0;
    a = keisan(m, (n - m));
    a += m;
  }
  cout << a << endl;
  return 0;
}
