#include <bits/stdc++.h>
using namespace std;
int N, X;
int cnt;
void solve(int a, int b) {
  if (a == 0 || b == 0) return;
  int minab = min(a, b);
  int maxab = max(a, b);
  cnt += 3 * minab;
  solve(maxab - minab, minab);
}
int main() {
  cin >> N >> X;
  cnt = 0;
  solve(N - X, X);
  cout << cnt << endl;
}
