#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 solve(i64 a, i64 b) {
  if (a < b) swap(a, b);
  if (b == 0) return -a;
  return a / b * b * 2 + solve(a % b, b);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int a, b;
  cin >> a >> b;
  cout << a + solve(a - b, b) << endl;
  return 0;
}
