#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int get_path_len(ll x, ll y) {
  if (x < y) {
    swap(x, y);
  }
  ll ret = 0;
  if (x % y == 0) {
    ret = ((x / y) * 2 - 1) * y;
  } else {
    ret = (x / y) * 2 * y;
  }
  if (x % y != 0) {
    ret += get_path_len(y, x % y);
  }
  return ret;
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << get_path_len(n - x, x) + x + n - x << endl;
  return 0;
}
