#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, s = 0;
  cin >> n;
  cin >> x;
  while (x != 0) {
    s += x;
    if (x >= n - x) {
      int a = x;
      x = n - x;
      n = a;
    } else {
      x = n - x;
    }
  }
  cout << s;
}
