#include <bits/stdc++.h>
using namespace std;
pair<int, int> add(int l, int s) {
  int q = l / s;
  int r = l % s;
  if (r == 0) return make_pair((2 * q - 1) * s, r);
  return make_pair(2 * q * s, r);
}
int main() {
  int N, X;
  cin >> N >> X;
  int res = 0;
  res += N;
  int s = min(X, N - X);
  int l = max(X, N - X);
  while (1) {
    pair<int, int> temp = add(l, s);
    res += temp.first;
    if (temp.second == 0) break;
    l = s;
    s = temp.second;
  }
  cout << res << endl;
}
