In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you are familiar with IRC chat, the support team is also reachable on PIRC network (
#szkopul channel. If you are not, just use email.
Please do not ask us things like "how to solve task XYZ?".
Please remember that the support team has to sleep sometimes or go to work in real life.
Byteman keeps all of his savings in an old safe. The lock of this safe consists of identical wheels, each of them has the same word consisting of letters written on it. Wheels can be rotated independently of each other (this way each wheel can be put in different positions). Safe gets opened, when for all positions letters written on all wheels for that position are the same.
Recently one of the Byteman's acquaintance told him that it is a good idea to keep money in a bank. Byteman decided to open his safe as soon as possible and transfer all of his money to a high-interest bank account. Assuming that rotation of any wheel by degrees to the left or to the right can be performed within one second, calculate, how long would it take (at minimum) to open Byteman's safe.
The first line of the standard input contains two integers and (), separated with a single space and denoting the number of wheels in the lock and the length of the word written on each wheel. The second line of the input contains one word of length , consisting of large English letters. In the third line there are integers (), separated with single spaces. means that the word from the 'th wheel is rotated by positions to the left (relatively to some defined initial position), which means it is set in position . For instance, if then the word on the 'th wheel is not rotated at all.
The first and only line of the standard output should contain one integer, the minimal time required to open Byteman's safe.
For the input data:
4 6 SLOWIK 2 0 3 5
the correct result is:
For the example lock, words written on each wheel look as follows:
For example, rotating the first wheel by one position to the left gives WIKSLO. When we rotate the same wheel to the right instead, we get LOWIKS.
Task author: Jakub Radoszewski.