#include <bits/stdc++.h>
using namespace std;
int n, m;
int xx, yy;
int ans;
int main() {
  cin >> n >> m;
  ans += n;
  xx = m;
  yy = n - m;
  if (xx < yy) swap(xx, yy);
  do {
    if (xx % yy != 0)
      ans += xx / yy * 2 * yy;
    else
      ans += (xx / yy * 2 - 1) * yy;
    xx = xx % yy;
    swap(xx, yy);
  } while (yy != 0);
  cout << ans << endl;
}
