798B.md (1427B)
1 Source: https://codeforces.com/problemset/problem/798/B 2 3 B. Mike and strings 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Mike has *n* strings *s*1, *s*2, ..., *s**n* each consisting of lowercase English letters. In one move he can choose a string *s**i*, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec". 22 23 Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal? 24 25 Input 26 27 The first line contains integer *n* (1 ≤ *n* ≤ 50) — the number of strings. 28 29 This is followed by *n* lines which contain a string each. The *i*-th line corresponding to string *s**i*. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50. 30 31 Output 32 33 Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution. 34 35 Examples 36 37 Input 38 39 ``` 40 4 41 xzzwo 42 zwoxz 43 zzwox 44 xzzwo 45 ``` 46 47 Output 48 49 ``` 50 5 51 ``` 52 53 Input 54 55 ``` 56 2 57 molzv 58 lzvmo 59 ``` 60 61 Output 62 63 ``` 64 2 65 ``` 66 67 Input 68 69 ``` 70 3 71 kc 72 kc 73 kc 74 ``` 75 76 Output 77 78 ``` 79 0 80 ``` 81 82 Input 83 84 ``` 85 3 86 aa 87 aa 88 ab 89 ``` 90 91 Output 92 93 ``` 94 -1 95 ``` 96 97 Note 98 99 In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".