#include <bits/stdc++.h>
using namespace std;
template <class V>
void printa(V& v) {
  for (auto& i : v) cout << i << " ";
  cout << "\n";
  ;
}
int main(void) {
  int n, x;
  cin >> n >> x;
  int len = 0;
  int d = n - x;
  len += x + d;
  while (true) {
    if (d > x) swap(d, x);
    int mv = x / d * 2;
    len += d * mv;
    int y = x % d;
    if (y == 0) {
      len -= d;
      break;
    } else {
    }
    x = x - d;
  }
  cout << len << "\n";
  return 0;
}
