#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int main() {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  if (n & 1) {
    printf("%lld\n", (n - 1) * 3);
  } else {
    m = min(m, n - m);
    long long k = max(gcd(n, m), n - gcd(n, m));
    printf("%lld\n", k * 3);
  }
}
