eolymp
bolt
Try our new interface for solving problems
Problems

Monkey at the Keyboard

Monkey at the Keyboard

The "Entertaining math" showmen decided to ensure their audience in the well-known fact that a monkey randomly pressing keyboard buttons will sooner or later type the required word according to the probability theory. The monkey taken to the shooting from the city zoo already can type --- every second she types one of \textbf{N} first letters of English alphabet with equal probability. Fortunately, the word prepared by the showmen also contains some of these \textbf{N} letters only. However, prior to giving a keyboard to the monkey, the showmen want to calculate the time it would take her to finish the job. More precisely, after how many seconds the given word will appear in the typed string for the first time? \InputFile The first line contains an integer \textbf{N} --- the number of letters the monkey can type (\textbf{1} ≤ \textbf{N} ≤ \textbf{26}). The second line contains a word proposed by the showmen. The word can contain only the first \textbf{N} lowercase Latin letters. Its length is positive and doesn't exceed \textbf{30000}. \OutputFile Output the expected time the monkey will need to type a word, rounded down to the nearest integer.
Time limit 5 seconds
Memory limit 64 MiB
Input example #1
2
aa
Output example #1
6
Author Igor Chevdar
Source Ural SU Contest. Petrozavodsk Summer Session, August 2008