#include <bits/stdc++.h>
typedef struct hoge {
  int y;
  int x;
  int res;
} hoge;
int main() {
  int n;
  int len;
  int swap;
  hoge a;
  scanf("%d %d", &n, &a.x);
  a.y = n - a.x;
  a.res = a.x + a.y;
  while (a.x) {
    swap = a.y;
    len = a.y / a.x;
    a.res += len * a.x * 2;
    a.y = a.x;
    a.x = swap % a.x;
  }
  a.res -= a.y;
  printf("%d\n", a.res);
  return 0;
}
