#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int x;
  cin >> n;
  cin >> x;
  int sum = n;
  int temp1;
  int lg = x;
  int sm = n - x;
  if (sm > lg) {
    temp1 = sm;
    sm = lg;
    lg = temp1;
  }
  while (true) {
    if (lg == sm) {
      sum = sum + x;
      break;
    }
    if (lg % sm == 0) {
      sum += ((2 * (lg / sm)) - 1) * sm;
      break;
    } else {
      sum += 2 * (lg / sm) * sm;
      temp1 = lg % sm;
      lg = sm;
      sm = temp1;
    }
  }
  cout << sum << endl;
  return 0;
}
