#include <bits/stdc++.h>
using namespace std;
int gcd(int m, int n) {
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}
int main() {
  int N, X;
  if (scanf("%d%d", &N, &X) < 2) return 0;
  printf("%d\n", 3 * (N - gcd(N, X)));
  return 0;
}
