#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch == '-' && (f = 0) || (ch = getchar());
  while (ch <= '9' && ch >= '0')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f ? x : -x;
}
int resolve(int a, int b) {
  if (a < b) swap(a, b);
  if (a % b == 0) return 2 * a - b;
  return 2 * b * (a / b) + resolve(b, a % b);
}
int main(int argc, char const *argv[]) {
  int n = read(), p = read();
  printf("%d\n", resolve(p, n - p) + n);
  return 0;
}
