#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
vector<int> a;
vector<int> b;
int cnt = 0;
int gcd(int x, int y) {
  if (y) {
    cnt++;
    a.push_back(y);
    b.push_back(x / y);
    return gcd(y, x % y);
  } else {
    return x;
  }
}
int main() {
  int N, K;
  cin >> N >> K;
  K = max(N - K, K);
  gcd(N, K);
  int ans;
  if (cnt == 1) {
    ans = a[0] * 3;
  } else if (cnt == 2) {
    ans = 1 * a[0];
    ans += (2 * b[1]) * a[1];
  } else {
    ans = 1 * a[0];
    ans += (2 * b[1] + 1) * a[1];
    for (int i = 2; i < a.size() - 1; i++) {
      ans += 2 * b[i] * a[i];
    }
    ans += (2 * b[a.size() - 1] - 1) * a[a.size() - 1];
  }
  cout << ans << endl;
  return 0;
}
