#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, i;
  scanf("%d %d", &n, &x);
  int a = n, b = n - x;
  if (a < b) swap(a, b);
  while (b != 0) {
    int x = a;
    a = b;
    b = x % b;
    if (a < b) swap(a, b);
  }
  printf("%d", 3 * (n - a));
}
