#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X;
  cin >> N >> X;
  pair<int, int> s = pair<int, int>(0, N - X);
  pair<int, int> u = pair<int, int>(0, N);
  pair<int, int> v = pair<int, int>(0, N);
  pair<int, int> w = pair<int, int>(0, N);
  pair<int, int> x = s;
  long long d = 0;
  long long z = 0;
  do {
    if (d == 0) {
      long long t1 = w.second - x.second;
      long long t2 = u.second;
      if (t1 < t2) {
        z += t1 - x.first;
        x.first = t1;
        v.second = x.second;
        d = 4;
      } else {
        z += t2 - x.first;
        x.first = t2;
        v.first = x.second;
        d = 2;
      }
    } else if (d == 1) {
      long long t1 = w.second - x.first;
      long long t2 = v.second;
      if (t1 < t2) {
        z += t1 - x.second;
        x.second = t1;
        u.second = x.first;
        d = 3;
      } else {
        z += t2 - x.second;
        x.second = t2;
        u.first = x.first;
        d = 5;
      }
    } else if (d == 2) {
      long long q = x.first + x.second;
      long long t1 = q - u.first;
      long long t2 = v.second;
      if (t1 < t2) {
        z += t1 - x.second;
        x.first = u.first;
        x.second = t1;
        w.first = q;
        d = 0;
      } else {
        z += t2 - x.second;
        x.first = q - v.second;
        x.second = v.second;
        w.second = q;
        d = 4;
      }
    } else if (d == 3) {
      long long t1 = w.first - x.second;
      long long t2 = u.first;
      if (t1 > t2) {
        z += x.first - t1;
        x.first = t1;
        v.first = x.second;
        d = 1;
      } else {
        z += x.first - t2;
        x.first = t2;
        v.second = x.second;
        d = 5;
      }
    } else if (d == 4) {
      long long t1 = w.first - x.first;
      long long t2 = v.first;
      if (t1 > t2) {
        z += x.second - t1;
        x.second = t1;
        u.first = x.first;
        d = 0;
      } else {
        z += x.second - t2;
        x.second = t2;
        u.second = x.first;
        d = 2;
      }
    } else if (d == 5) {
      long long q = x.first + x.second;
      long long t1 = q - u.second;
      long long t2 = v.first;
      if (t1 > t2) {
        z += x.second - t1;
        x.first = u.second;
        x.second = t1;
        w.second = q;
        d = 3;
      } else {
        z += x.second - t2;
        x.first = q - v.first;
        x.second = v.first;
        w.first = q;
        d = 1;
      }
    }
  } while (x != s);
  cout << z << endl;
  return 0;
}
