#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k, ans = 0, z;
  cin >> n >> k;
  ans += n;
  long long x = max(k, n - k), y = min(k, n - k);
  while (y) {
    ans += (x / y) * y + y;
    z = x % y;
    x = y;
    y = z;
  }
  cout << ans << "\n";
}
