#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, INF = 1e9;
using vi = vector<long long>;
using vvi = vector<vi>;
using int64 = long long;
signed main() {
  long long n, x;
  cin >> n >> x;
  if (x > n - x) x = n - x;
  vector<long long> ret;
  ret.push_back(1);
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) {
      ret.push_back(i);
      ret.push_back(x / i);
    }
  ret.push_back(x);
  long long sa;
  for (auto itr = ret.begin(); itr != ret.end(); itr++) {
    if (n % *itr == 0) sa = max(sa, *itr);
  }
  cout << n + n - sa + n - sa - sa << endl;
}
