#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;
set<long long> va(long long n) {
  set<long long> ret;
  ret.insert(1);
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) ret.insert(i);
  ret.insert(n);
  return (ret);
}
signed main() {
  long long n, x;
  cin >> n >> x;
  if (x > n - x) x = n - x;
  set<long long> varisa = va(x);
  long long sa;
  for (auto itr = varisa.begin(); itr != varisa.end(); itr++)
    if (n % *itr == 0) sa = *itr;
  cout << n + n - sa + n - sa - sa << endl;
}
