Question
Minimum Edits for a Repeating Password Pattern

A security system requires passwords to follow a repeating pattern with some period P: that is, the character at position i must always equal the character at position i + P, for every valid i.

You are given a lowercase string S of length N and a target period P. Find the minimum number of characters in S that must be changed (each change replaces one character with any other lowercase letter) so that the resulting string satisfies the period-P repeating property.

Input

The first line contains the string KaTeX can only parse string typed expression.

The second line contains a single integer KaTeX can only parse string typed expression — the target period.

Output

Print a single integer — the minimum number of character changes needed.

Example
Example 1:
Input
abcabc

3
Output
0
Explanation
The string already satisfies the period-3 property: position 0 matches position 3 ('a'='a'), position 1 matches position 4 ('b'='b'), and position 2 matches position 5 ('c'='c'). No changes are needed.

Example 2:
Input
aabbaa

2
Output
2
Explanation
Positions KaTeX can only parse string typed expression (period-2 residue class starting at 0) hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Positions KaTeX can only parse string typed expression hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Total: KaTeX can only parse string typed expression.

Example 3:
Input
a

1
Output
0
Explanation
A single character trivially satisfies any period, since there is no second position to compare it against.

Online