You are given a string S of length N made up of 0 and 1 only, together with an integer M. M divides N.
Three operations can be applied to S.
- Flip one character. A 0 becomes a 1, and a 1 becomes a 0.
- Choose a positive integer k and flip the first k×M characters. Here k×M≤N must hold.
- Choose a positive integer k and flip the last k×M characters. Here k×M≤N must hold.
Flipping the first N characters and flipping the last N characters give the same result, so they count as one operation.
For example, if S is "110100001001" and M=4, there are 17 operations that can be applied to S. Flipping the second character gives "100100001001", flipping the first 2×M characters gives "001011111001", and flipping the last M characters gives "110100000110".
Write a program that finds the minimum number of operations needed to turn every character of S into 1.