Scanf.scanf "%d %d" (fun n m ->
    let xyz = Array.init n (fun _ -> Scanf.scanf " %d %d %d" (fun x y z -> x, y, z)) in
    let (@|) a b = max a b in

    let f a b c =
        Array.sort (fun (x0, y0, z0) (x1, y1, z1) ->
            compare (x0 * a + y0 * b + z0 * c) (x1 * a + y1 * b + z1 * c)) xyz;
        let rec loop r i x y z =
            if r = m then abs x + abs y + abs z else
                let (x0, y0, z0) = xyz.(i) in
                loop (r + 1) (i - 1) (x + x0) (y + y0) (z + z0)
        in
        loop 0 (n - 1) 0 0 0
    in

    let p, m = 1, -1 in

    f p p p @| f p p m @|
    f p m p @| f p m m @|
    f m p p @| f m p m @|
    f m m p @| f m m m |> Printf.printf "%d\n"
)