#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  int n, x, y;
  int i, j, k;
  int s = 0;
  cin >> n >> x;
  s = n;
  y = n - x;
  if (y < x) swap(x, y);
  while (x != 0) {
    a = y / x;
    s += a * x * 2;
    y %= x;
    swap(x, y);
  }
  s -= y;
  cout << s << endl;
  return 0;
}
