#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
int f(int x, int y) {
  int mx = max(x, y);
  int mn = min(x, y);
  if (mx % mn == 0) return (mx / mn) * (2 * mn) - mn;
  return f(mn, mx % mn) + (mx / mn) * (2 * mn);
}
int main() {
  int n, x;
  cin >> n >> x;
  int ans = x + (n - x) + f(x, n - x);
  cout << ans << endl;
  return 0;
}
