import Foundation

let input = readLine()!.componentsSeparatedByString(" ").map{ Int($0)! }

let mod = input[0] % input[1]
let min = abs(mod - input[1])
if(mod >= min){
  print(min)
}else{
  print(mod)
}