let arr=readLine()!.characters.split{$0==" "}.map{Int(String($0))!}
var a=arr[0]
var b=arr[1]
var x = 0
var y = 0
while(true){
    x = a
    if(b==1){
       x = 0
       break
     }
  a -= b
  
  if(a==0)
  {
    x = a
    break
  }else{
    if(0<a){
      x = a
    }else if (a<0){
      y = abs(a)
      if(x<y){
        break
      }else{
        x = y
        break
      }
    }
  }
}
print(String(x))