#include <bits/stdc++.h>
using namespace std;
long long int n, m, a = 0, memo[1000006];
vector<long long int> v, ans;
long long int keisan(long long int wra, long long int wru) {
  if (wra == 0)
    return 0;
  else if (memo[wru] != 0)
    return memo[wru];
  else if (wra < wru)
    return memo[wru] = keisan(wru, (wru - wra)) - (wru - wra);
  else {
    return memo[wru] = keisan((wra % wru), wru) + (wra / wru) * wru * 2;
  }
}
signed main() {
  cin >> n >> m;
  if (n % 2 == m) {
    a = m * 3;
  } else if (n / 2 >= m) {
    a = keisan((n - m), m);
    a += m + (n - m);
  } else {
    a = m + keisan((n - m), m);
  }
  cout << a << endl;
  return 0;
}
