#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct { int to, cost; };
const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll solve(ll x, ll y) {
  if (x == 0 || y == 0) return 0;
  if (x < y)
    return solve(x, y - x) + x * 2;
  else if (x > y)
    return solve(x - y, y) + y * 2;
  else
    return x;
}
int main() {
  ll N, X;
  cin >> N >> X;
  cout << N + solve(N - X, X) << endl;
  return 0;
}
