#include <bits/stdc++.h>
using namespace std;
int solve(int a, int b) {
  int r = a % b;
  int d = a / b;
  if (r) {
    return 2 * d * b + solve(b, r);
  }
  d--;
  return 2 * d * b + b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n >> x;
  int a = max(x, n - x);
  int b = min(x, n - x);
  cout << solve(a, b) + n << endl;
  return 0;
}
