Scanf.scanf "%d %d" (fun h w ->
    let step2 hw0 h1 w1 =
        let hw1 = h1 * w1 in
        let hw2 = h * w - hw0 - hw1 in
        max hw0 (max hw1 hw2) - min hw0 (min hw1 hw2)
    in
    let step1 h0 w0 =
        let hw = h0 * w0 in
        if w0 = w then (
            let h1 = h - h0 in
            let a1 = step2 hw h1 (max 1 ((w0 + 0) / 2)) in
            let a2 = step2 hw h1 (max 1 ((w0 + 1) / 2)) in
            let a3 = step2 hw (max 1 ((h1 + 0) / 2)) w0 in
            let a4 = step2 hw (max 1 ((h1 + 1) / 2)) w0 in
            min (min a1 a2) (min a3 a4)
        ) else (
            let w1 = w - w0 in
            let a1 = step2 hw h0 (max 1 ((w1 + 0) / 2)) in
            let a2 = step2 hw h0 (max 1 ((w1 + 1) / 2)) in
            let a3 = step2 hw (max 1 ((h0 + 0) / 2)) w1 in
            let a4 = step2 hw (max 1 ((h0 + 1) / 2)) w1 in
            min (min a1 a2) (min a3 a4)
        )
    in
    let a1 = step1 (max 1 ((h + 0) / 3)) w in
    let a2 = step1 (max 1 ((h + 2) / 3)) w in
    let a3 = step1 h (max 1 ((w + 0) / 3)) in
    let a4 = step1 h (max 1 ((w + 2) / 3)) in
    min (min a1 a2) (min a3 a4) |> Printf.printf "%d\n"
)