#include <bits/stdc++.h>
using namespace std;
int ans;
int dfs(int x, int y) {
  if (!y) return x;
  ans += x / y * y * 2;
  return dfs(y, x % y);
}
int main() {
  int n, x, y;
  scanf("%d%d", &n, &x);
  ans = n;
  y = dfs(max(x, n - x), min(x, n - x));
  cout << ans - y;
}
