#include <bits/stdc++.h>
using namespace std;
int john(int m, int n) {
  int ans;
  if (n % m == 0)
    return n * 2 - m;
  else {
    ans = n / m * m * 2 + john(n % m, m);
    return ans;
  }
}
int main() {
  int n, m;
  int ans;
  cin >> n >> m;
  ans = john(m, n - m) + n;
  cout << ans << endl;
  return 0;
}
