#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, ans, a, b, c, d, e, f, tmpa, tmpb, tmpc;
  cin >> n >> x;
  ans = x;
  a = n - x;
  b = 0;
  c = n;
  d = 0;
  e = n - x;
  f = x;
  while (true) {
    if (c == a) {
      ans += b;
      ans += a;
      break;
    } else if (c > a) {
      ans += b;
      ans += a;
      tmpa = a, tmpb = b, tmpc = c;
      a = tmpc - tmpa;
      b = d;
      c = e;
      d = f;
      e = 0;
      f = tmpb + tmpa;
    } else {
      ans += b;
      ans += c;
      tmpa = a, tmpb = b, tmpc = c;
      a = tmpb + tmpc;
      b = 0;
      c = tmpa;
      d = tmpb;
      e = tmpc;
      f = tmpa - tmpc;
    }
  }
  cout << ans << endl;
}
