#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int BASE = 27;
const int oo = 1e9;
const double EPS = 1e-9;
const int MX = 1e5 + 1;
long long f(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return -b;
  return 2 * a + f(a, b - a);
}
long long n, first;
int main() {
  cin >> n >> first;
  cout << (n + f(first, n - first)) << endl;
  return 0;
}
