#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, X;
  cin >> N >> X;
  int raypoint[2];
  int a = X;
  int b = N - X;
  if (a < b) swap(a, b);
  int ans = N;
  while (b) {
    int k = a / b;
    ans += 2 * a;
    int l = b % a;
    a = l;
    b = k;
  }
  ans -= a;
  cout << ans << endl;
  return 0;
}
