#include <bits/stdc++.h>
using namespace std;
long long int n, m, i, j, k, l, t, a, b;
int main() {
  scanf("%lld %lld", &n, &m);
  j = 2 * m;
  if (j <= n) {
    t = n + j;
    a = m;
    b = n - j;
    while (a % b != 0) {
      n = a / b;
      n *= b;
      t += (n * 2);
      n = a % b;
      a = b;
      b = n;
    }
    n = a / b;
    m = ((n - 1) * 2) + 1;
    t += (m * b);
    printf("%lld\n", t);
  } else {
    t = n + ((n - m) * 2);
    a = n - m;
    b = n - ((n - m) * 2);
    while (a % b != 0) {
      n = a / b;
      n *= b;
      t += (n * 2);
      n = a % b;
      a = b;
      b = n;
    }
    n = a / b;
    m = ((n - 1) * 2) + 1;
    t += (m * b);
    printf("%lld\n", t);
  }
}
