#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, ans, a, b, tmpa;
  cin >> n >> x;
  ans = x;
  a = x;
  b = n - x;
  while (true) {
    ans += b;
    if (a == b) {
      ans += a;
      break;
    } else if (a > b) {
      ans += b;
      a = a - b;
    } else {
      tmpa = a;
      a = b;
      b = tmpa;
      ans += b;
      a = a - b;
    }
  }
  cout << ans << endl;
}
