#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  long long a, b;
  cin >> a >> b;
  long long out = a;
  int x = b, y = a - b;
  while (1) {
    if (x == y) {
      out += x;
      break;
    }
    if (x > y) {
      long long t = x / y - 1;
      if (t == 0) t = 1;
      out += y * 2 * t;
      x -= y * t;
    } else {
      long long t = y / x - 1;
      if (t == 0) t = 1;
      out += x * 2 * t;
      y -= x * t;
    }
  }
  cout << out << endl;
}
