let population_count : int -> int = fun bits ->
  List.fold_left (fun bits (mask, shamt) ->
    (mask land bits) + (mask land (bits lsr shamt))) bits
    [ (0x5555555555555555, 1);
      (0x3333333333333333, 2);
      (0x0f0f0f0f0f0f0f0f, 4);
      (0x00ff00ff00ff00ff, 8);
      (0x0000ffff0000ffff, 16);
      (0x00000000ffffffff, 32) ]

module ABSet = Set.Make (struct
  type t = int * int
  let compare (a, b) (a', b') =
    compare (population_count a, -b, a) (population_count a', -b', a')
end)

let () =
  let n, k = Scanf.scanf "%d %d\n" (fun n k -> n, k) in
  let abs = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  Array.fold_left (fun m (a, b) ->
    ABSet.fold (fun (a', b') m ->
      if (a lor a') <= k then begin
        match ABSet.split (a lor a', b + b') m with
        | _, true, _ -> m
        | minor, false, _ ->
            if ABSet.for_all (fun (a'', b'') -> a'' land lnot (a lor a') <> 0 || b'' < b + b') minor then ABSet.add (a lor a', b + b') m else m end else m) m m) (ABSet.singleton (0, 0)) abs
  |> (fun m -> ABSet.fold (fun (_, b) b' -> max b b') m 0)
  |> Printf.printf "%d\n"