#include using namespace std; int main() { int n, d; cin >> n >> d; int x,d2; x = n%d; while (x != 1) { if (x == 0) { cout << n/d - 1 << endl; return 0; } d2 = x; x = d%x; d = d2; } cout << n - 1 << endl; return 0; }