#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long N, X;
  cin >> N >> X;
  long long ret = N;
  long long left1 = N - X;
  long long left2 = X;
  long long st = 0;
  while (left1 > 0) {
    if (st == 0) {
      if (left1 == left2) {
        ret += left1;
        break;
      }
      if (left1 > left2) {
        ret += left2;
        left1 -= left2;
        st = 1;
      } else {
        ret += left1;
        left2 -= left1;
        st = 2;
      }
    } else if (st == 1) {
      ret += left2;
      st = 0;
    } else if (st == 2) {
      ret += left1;
      st = 0;
    }
  }
  cout << ret << endl;
  return 0;
}
