#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int res = 0;
  int neg;
  while (true) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9' || ch == '-') {
      if (ch == '-')
        neg = -1;
      else
        neg = 1, res = ch - '0';
      break;
    }
  }
  while (true) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9')
      res *= 10, res += ch - '0';
    else
      break;
  }
  return res * neg;
}
const int maxn = 100020;
const int maxm = 1000020;
const int MOd = 1e9 + 7;
const int K = 320;
int a, b;
int main() {
  cin >> a >> b;
  long long ans = a;
  a -= b;
  if (a > b) swap(a, b);
  while (a) {
    ans += (b / a) * a * 2;
    b %= a;
    if (b == 0) {
      ans -= a;
      break;
    }
    swap(a, b);
  }
  cout << ans << endl;
  return 0;
}
