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