#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, l = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') l = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - 48;
  return x * l;
}
long long n, x, a, d, ans;
void opp() {
  ans += 2 * min(a, d);
  if (a > d)
    a -= d;
  else
    d -= a;
}
int main() {
  ans = n = read();
  d = x = read();
  a = n - x;
  while (a != d) opp();
  printf("%lld", ans + a);
  return 0;
}
