#include <bits/stdc++.h>
using namespace std;
long test(long nokori, long now, long sum) {
  long count_node, tmp = 0;
  if (nokori % now != 0) {
    count_node = nokori / now;
    count_node = count_node * 2;
    count_node = now * count_node;
    sum = sum + count_node;
    tmp = nokori;
    nokori = now;
    now = tmp % now;
  } else if (nokori % now == 0) {
    count_node = nokori / now;
    count_node = count_node * 2 - 1;
    count_node = count_node * now;
    sum = sum + count_node;
    now = 0;
    return count_node;
  }
  if (now != 0) {
    sum = sum + test(nokori, now, sum);
  }
  return sum;
}
int main() {
  long x, n;
  cin >> n >> x;
  long sum, now, nokori = 0;
  nokori = n - x;
  sum = n;
  now = x;
  sum = test(nokori, now, sum);
  cout << sum << "\n";
  return 0;
}
