cho.sh
Notes
Loading...

Card Flipping

Time limit

5s

Memory limit

128 MB

Problem

There is an array of cards with R rows and 16 columns. Every card initially shows its front side. Each cell is marked with either 0 or 1. Cards marked 0 must end face up, and cards marked 1 must end face down.

In one operation, you may choose a contiguous group of cards in one row, or a contiguous group of cards in one column, and flip all chosen cards. A flipped card changes between front and back.

Find the minimum number of operations needed to reach the target state. Minimize the number of operations, not the total number of flipped cards.

Input

The first line contains the number of rows R (1 <= R <= 50). Each of the next R lines contains a string of length 16. Every character is either 0 or 1. A 0 means the card must end face up, and a 1 means the card must end face down.

Output

Print the minimum number of flip operations needed to reach the target state.