open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

open InputUtility

module ARC074 =
    let private ``Chocolate Bar`` () = 
        let gr r a =
            Seq.max a - Seq.min a
            |> min r
        let check a b =
            let rec inner b c r =
                if c > a then r
                else
                    let nr =
                        let d = a - c
                        seq {
                            yield [b * c; d / 2L * b; (d + 1L) / 2L * b];
                            yield [b * c; b / 2L * d; (b + 1L) / 2L * d];
                        } |> Seq.fold gr r
                    inner b (c + 1L) nr
            inner b 1L System.Int64.MaxValue
        let g = ReadNext >> int64
        let h, w = g (), g ()
        min (check h w) (check w h)
        |> printfn "%d"

    let Entry = ``Chocolate Bar``

ARC074.Entry ()