#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, X;
  cin >> N >> X;
  int ans = X;
  int a = X;
  int b = N - X;
  while (1) {
    if (a < b) {
      ans += b;
      int temp = b;
      b = a;
      a = temp;
      continue;
    }
    if (a % b == 0) {
      int c = (a / b) * 2 - 1;
      ans += c * b;
      break;
    } else {
      int temp = a % b;
      int c = (a / b) * 2;
      ans += c * b;
      a = b;
      b = temp;
    }
  }
  cout << ans << endl;
  return (0);
}
