import Foundation

let input = readLine()!.componentsSeparatedByString(" ").map{ Int($0)! }
for i in 0...(input[1]){
  if( (input[0] - i) % input[1] == 0){
    print(i)
    break
  }else if( (input[0] + i) % input[1] == 0){
    print(i)
    break
  }
}