#include <bits/stdc++.h>
using namespace std;
inline 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 * 10 + c - 48;
  return x * l;
}
long long n, x, ans;
int main() {
  ans = n = read();
  x = read();
  long long la = x, lb = n - x;
  while (la != lb)
    if (la > lb)
      ans += 2 * lb, la -= lb;
    else
      ans += 2 * la, lb -= la;
  cout << ans + la;
  return 0;
}
