#include using namespace std; int main(){ int n,d,nc,dc; cin >> n >> d; if(d == 0){ printf("0\n"); }else{ nc = n;dc = d; while(nc!=dc){ if(nc>dc)nc-=dc; else dc-=nc; } printf("%d\n",n-((dc-1)*(n/dc)+1)); } }