// 🐋💭
import Foundation

func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main() {
    let nk = readInts()
    //let (n, k) = (nk[0], nk[1])
    var n = nk[0]
    let k = nk[1]
    //__________________________________ここまで入力処理
    if n == 1 {
        print(0)
        return
    }
    while n > abs(n - k) {
        n = abs(n - k)
    }
    print(n)
}
main()

