#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 17;
const int MAXN = maxn * 4;
const int darr = 1e3 + 17;
const long long INF = 1e17 + 5;
const int mod = 1e9 + 7;
void Need_For_Speed() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long n, x;
long long get(long long n, long long x) {
  if (n % x == 0) {
    return n + n - x;
  }
  return (n - (n % x)) * x + n + get(x, n % x);
}
int main() {
  Need_For_Speed();
  cin >> n >> x;
  cout << get(n - x, x) + x;
  return 0;
}
