This page is still under construction.

Parts of this page are still being built. What you see may change.

Parity

Time limit10sMemory limit512 MB

Summary
Given n binary strings and target bits, find the smallest column subset of size at most k whose XOR over each string matches its bit.
Level

Medium7 of 10

Topics
Bit manipulation, Greedy, Brute force, Math
Solved
No attempts yet

Problem

You are given nn binary strings s1,…,sns_1, \dots, s_n, all of the same length mm. Each string sis_i comes with a bit bib_i. The strings are 00-indexed.

For a subset S⊆{0,1,…,m−1}S \subseteq \{0, 1, \dots, m-1\} of column indices, define the value it produces on string sis_i as the XOR of the bits of sis_i at the positions in SS. (The XOR of a set of bits is 11 if an odd number of them are 11, and 00 otherwise; the XOR of the empty set is 00.)

You are given a nonnegative integer kk. You want to choose a subset SS of size at most kk so that for every i=1,…,ni = 1, \dots, n, the value produced on sis_i equals bib_i.

For example, if s1=1010s_1 = 1010 and S={0,3}S = \{0, 3\}, then the value on s1s_1 is 11 (the bit at index 00) XOR 00 (the bit at index 33), which is 11.

Among all valid subsets SS of size at most kk, output the minimum possible size. If no such subset exists, report that instead.

Input

The first line contains two space-separated integers nn and kk (1≤n≤641 \le n \le 64, 0≤k≤100 \le k \le 10).

Each of the next nn lines contains a string sis_i, a single space, and its bit bib_i. All strings have the same length mm (1≤m≤501 \le m \le 50), and k≤mk \le m.

Output

Output a single integer: the minimum possible size of a subset S⊆{0,1,…,m−1}S \subseteq \{0, 1, \dots, m-1\} with ∣S∣≤k|S| \le k such that the XOR of the bits of sis_i over the indices in SS equals bib_i for every ii. If no such subset exists, output −1-1.

Examples5

  1. Example 1

    Input
    3 1
    111 1
    001 0
    011 1
    
    Expected output
    1
    
  2. Example 2

    Input
    4 7
    010001000111100011010010000011110000000000 0
    001101010101000101001011110001101010101111 0
    100111100101100000110000110110010110100101 0
    011010001110101111000000111101100010111111 1
    
    Expected output
    1
    
  3. Example 3

    Input
    2 3
    10 0
    01 0
    
    Expected output
    0
    
  4. Example 4

    Input
    2 2
    10 1
    01 1
    
    Expected output
    2
    
  5. Example 5

    Input
    2 0
    10 1
    01 0
    
    Expected output
    -1