Substring appearing twice

Given a string and at most K letter replacements, maximize the length of the longest substring that occurs at two different starting positions, allowing overlap.

Hard8StringBinary searchDynamic programmingString matchingNo attempts yetTime limit6sMemory limit128 MB

Problem

You are given a string SS of lowercase English letters.

Define the following value: the length of the longest substring that occurs at two different starting positions in SS. The two occurrences may overlap. If no such substring exists, the value is 00.

You may replace a letter of SS with any other lowercase letter, and you may do this at most KK times. After every replacement is made, the value above is computed on the resulting string. Find the largest value you can reach.

Replacements apply to the string itself, once. When the two occurrences overlap, the letters inside the overlap are shared by both occurrences.

Input

The first line contains the integer KK (0K50000 \le K \le 5000).

The second line contains the string SS, made of lowercase English letters only (1S50001 \le |S| \le 5000).

Output

Print one integer, the largest value reachable with at most KK replacements.

Note

Take K=1K = 1 and SS equal to abcba. Changing the third letter c to a gives ababa, where the substring aba occurs at position 0 and at position 2. The answer is 3.