#include<stdio.h>

int main() {
    long long int a,b,x;
    scanf("%lld %lld",&a,&b);
    if(b==1){
        printf("0\n");
    }
    else if(a<b){
        printf("%lld\n",a);
    }
    else {
        while(a>b){
            x = a-b;
            a = x;
        }
        printf("%lld\n",b-a);
    }


    return 0;
}
