#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, length;
  int a[2];
  cin >> n >> x;
  a[0] = n - x;
  a[1] = x;
  length = x;
  while (a[0] != a[1]) {
    if (a[0] > a[1]) {
      length = length + ((a[0] - 1) / a[1]) * a[1];
      a[0] = (a[0] - 1) % a[1] + 1;
    }
    if (a[0] < a[1]) {
      a[1] = (a[1] - 1) % a[0] + 1;
    }
  }
  cout << length * 3;
}
