#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool used[110];
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first * a.first + a.second * a.second >
         b.first * b.first + b.second * b.second;
}
void solve(int n) {
  vector<pair<int, int>> p(n), q(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  for (int i = 0; i < n; i++) {
    cin >> q[i].first >> q[i].second;
  }
  int dist = 80000, flag = 0;
  int ans = 0;
  sort(p.begin(), p.end());
  for (int i = 0; i < n; i++) {
    dist = 80000;
    flag = 0;
    for (int j = 0; j < n; j++) {
      if (!used[j]) {
        int x = q[j].first - p[i].first;
        int y = q[j].second - p[i].second;
        if ((x > 0) && (y > 0) && y < dist) {
          dist = y;
          flag = j;
        }
      }
    }
    if (dist < 80000) {
      ans++;
      used[flag] = true;
    }
  }
  cout << ans << endl;
}
char board[110][110];
int main(void) {
  long long n, x, ans = 0;
  cin >> n >> x;
  if (n == 2 * x) {
    cout << n + x << endl;
    return 0;
  }
  ans = n;
  long long l1 = n - x;
  long long l2 = x;
  bool flag = true;
  while (l2 != 0) {
    if (l1 % l2 != 0) {
      int num = l1 / l2;
      ans += l2 * num * 2;
      long long d = l1 - l2 * num;
      l1 = l2;
      l2 = d;
    } else {
      int num = l1 / l2;
      ans += l2 * (num * 2 - 1);
      long long d = l1 - l2 * num;
      l1 = l2;
      l2 = d;
    }
  }
  cout << ans << endl;
  return 0;
}
