798_B. Mike and strings_9716
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, 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".

Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.

This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

Examples

Input

4
xzzwo
zwoxz
zzwox
xzzwo


Output

5


Input

2
molzv
lzvmo


Output

2


Input

3
kc
kc
kc


Output

0


Input

3
aa
aa
ab


Output

-1

Note

In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
import copy
from collections import deque
from sys import stdin, stdout
from sys import maxsize
input = stdin.readline
# def print(n):
#    stdout.write(str(n)+'\n')


def solve():
    pass


def cycle(x):
    l = len(x)
    temp = copy.deepcopy(x)
    cnt = 1
    val = x.popleft()
    x.append(val)
    while(cnt < l and x != temp):
        val = x.popleft()
        x.append(val)
        cnt += 1
    return cnt


def check(temp, x):
    l = len(x)
    cnt = 0
    while(cnt < l and x != temp):
        val = x.popleft()
        x.append(val)
        cnt += 1
    if(cnt == l):
        return -1
    return cnt


test = 1
# test = int(input().strip())
for t in range(0, test):
    n = int(input().strip())
    a = []
    c = [0]*n

    flag = False
    for i in range(n):
        # String Input, converted to mutable list.
        s = deque(input().strip())
        a.append(s)
    template = a[0]
    distance = [0]*n
    for i in range(1, n):
        distance[i] = check(template, a[i])
        if(distance[i] == -1):
            print(-1)
            flag = True
            break
    for i in range(n):
        c[i] = cycle(a[i])
    if(flag):
        break
    ans = maxsize
    for i in range(len(a[0])+1):
        temp = 0
        for j in range(n):
            temp += (distance[j]+i) % c[j]
        ans = min(ans, temp)
    print(ans)
    # n, k = list(map(int, input().strip().split()))
    # arr = [int(x) for x in input().strip().split()]
    # brr = [list(map(int,input().strip().split())) for i in range(rows)]              # 2D array row-wise input
    ans = solve()


'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)]                                         # 2D array initialization
rev_str=s[::-1]                                                               # To reverse given string
b=input().strip().split()                                                     # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)]          # 2D array Linear Input
rows,cols=len(brr),len(brr[0])                                                # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1])                                               # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set()                                                                       # empty set
a=maxsize                                                                     # initializing infinity
b=-maxsize                                                                    # initializing -infinity
mapped=list(map(function,input))                                              # to apply function to list element-wise
try:                                                                          # Error handling
    #code 1
except:                                                                       # ex. to stop at EOF
    #code 2 , if error occurs
'''

4
xzzwo
zwoxz
zzwox
xzzwo
 3
kc
kc
kc
 3
aa
aa
ab
 2
molzv
lzvmo
 2
a
b
 2
abcd
cabd
 4
abcabcabc
bcabcabca
cabcabcab
cabcabcab
 2
dzlisvouhbqogzusikmkuvkql
qogzusikmkuvkqldzlisvouhb
 2
abab
aabb
 2
aabb
abab
 3
aaa
aaa
aaa
 3
aabbbaba
abaabbab
bbbaaaba
 4
xwppaubrphxjwmwfwypvwwjzotyobpiynyka
ubrphxjwmwfwypvwwjzotyobpiynykaxwppa
wjzotyobpiynykaxwppaubrphxjwmwfwypvw
tyobpiynykaxwppaubrphxjwmwfwypvwwjzo
 2
nzxv
zvnx
 12
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
 5
aaaa
aaaa
aaaa
aaaa
aaaa
 7
aaa
aab
aba
abb
baa
bab
bba
 2
njtazaab
abnjtaza
 4
zumixjfqhbkeg
kegzumixjfqhb
hbkegzumixjfq
gzumixjfqhbke
 2
xyxy
xxyy
 4
aa
aa
bb
bb
 2
aabaab
baabaa
 2
adam
mdaa
 38
kmlzdcnm
mlzdcnmk
lzdcnmkm
kmlzdcnm
lzdcnmkm
zdcnmkml
zdcnmkml
mlzdcnmk
zdcnmkml
mlzdcnmk
lzdcnmkm
zdcnmkml
kmlzdcnm
lzdcnmkm
zdcnmkml
mlzdcnmk
kmlzdcnm
mkmlzdcn
lzdcnmkm
nmkmlzdc
zdcnmkml
nmkmlzdc
kmlzdcnm
mlzdcnmk
mkmlzdcn
dcnmkmlz
cnmkmlzd
cnmkmlzd
mkmlzdcn
cnmkmlzd
dcnmkmlz
kmlzdcnm
nmkmlzdc
nmkmlzdc
kmlzdcnm
kmlzdcnm
lzdcnmkm
zdcnmkml
 2
aaabb
ababa
 2
abc
cba
 15
kknrrejishjz
hilbaftsfcaq
lncsgtjqgwjz
athvctulbhmb
nfvsjyiulmmr
hxjnvumwnwtr
rncsxqvkvqeg
qoabapuhodxk
ylinhbhyqjsn
cnzxgdgytgav
xufmacyangpv
hwvzionkdmjl
spklymjxiolk
qjkfrccaayak
onwrbgfvxrjx
 2
aaabb
baaba
 2
abcd
bdac
 2
aabc
acab
 3
aa
aa
aa
 10
ab
bc
cd
de
ef
gh
hi
ij
ik
mn
 3
aaaa
aaaa
aaaa
 2
abbc
abcc
 2
abcde
dcabe
 1
umwnrjtcytnquvdmqfiqt
 3
abcabc
bcabca
bcabca
 3
kwkb
kbkw
bkwk
 2
abc
acb
 2
abcabc
cabcab
 2
aa
aa
 2
abc
bac
 33
nkgcmrfvxe
vxenkgcmrf
rfvxenkgcm
vxenkgcmrf
xenkgcmrfv
enkgcmrfvx
enkgcmrfvx
nkgcmrfvxe
kgcmrfvxen
cmrfvxenkg
cmrfvxenkg
xenkgcmrfv
rfvxenkgcm
rfvxenkgcm
nkgcmrfvxe
xenkgcmrfv
rfvxenkgcm
xenkgcmrfv
xenkgcmrfv
gcmrfvxenk
mrfvxenkgc
fvxenkgcmr
vxenkgcmrf
enkgcmrfvx
cmrfvxenkg
cmrfvxenkg
mrfvxenkgc
kgcmrfvxen
vxenkgcmrf
enkgcmrfvx
cmrfvxenkg
cmrfvxenkg
gcmrfvxenk
 2
ac
bb
 2
abcd
bdca
 11
xdngtxuqjalamqvotuhx
otuhxxdngtxuqjalamqv
gtxuqjalamqvotuhxxdn
dngtxuqjalamqvotuhxx
votuhxxdngtxuqjalamq
xxdngtxuqjalamqvotuh
alamqvotuhxxdngtxuqj
uqjalamqvotuhxxdngtx
qjalamqvotuhxxdngtxu
hxxdngtxuqjalamqvotu
jalamqvotuhxxdngtxuq
 2
ab
aa
 5
aaaaa
aabaa
aaaaa
aaaaa
aaaaa
 2
oiadfnwpdcxxhbwwqbrcdujcusgtkqdjmintwjlb
brcdujcusgtkqdjmintwjlboiadfnwpdcxxhbwwq
 1
a
 20
cynedh
nedhcy
hcyned
cynedh
ynedhc
ynedhc
nedhcy
nedhcy
nedhcy
hcyned
nedhcy
hcyned
nedhcy
dhcyne
ynedhc
edhcyn
dhcyne
ynedhc
cynedh
cynedh
 2
sfotivvfgbdfcnvaybxhstavaoktatflelpyi
sfotivvfgbdfcnvaybxhstavaoktatflelpyi
 3
aab
abb
bab
 12
ktwwduoopsnkhfklrskdxakbmqhl
lktwwduoopsnkhfklrskdxakbmqh
duoopsnkhfklrskdxakbmqhlktww
klrskdxakbmqhlktwwduoopsnkhf
oopsnkhfklrskdxakbmqhlktwwdu
opsnkhfklrskdxakbmqhlktwwduo
kbmqhlktwwduoopsnkhfklrskdxa
lrskdxakbmqhlktwwduoopsnkhfk
wduoopsnkhfklrskdxakbmqhlktw
klrskdxakbmqhlktwwduoopsnkhf
hfklrskdxakbmqhlktwwduoopsnk
dxakbmqhlktwwduoopsnkhfklrsk
 9
rgycrkgcjktfdjkffcnlnhiawq
awqrgycrkgcjktfdjkffcnlnhi
rkgcjktfdjkffcnlnhiawqrgyc
jktfdjkffcnlnhiawqrgycrkgc
cjktfdjkffcnlnhiawqrgycrkg
fdjkffcnlnhiawqrgycrkgcjkt
ffcnlnhiawqrgycrkgcjktfdjk
ktfdjkffcnlnhiawqrgycrkgcj
wqrgycrkgcjktfdjkffcnlnhia
 15
gnizfqwqmimtgmtf
mtgmtfgnizfqwqmi
gmtfgnizfqwqmimt
zfqwqmimtgmtfgni
zfqwqmimtgmtfgni
fqwqmimtgmtfgniz
imtgmtfgnizfqwqm
fgnizfqwqmimtgmt
gmtfgnizfqwqmimt
mtgmtfgnizfqwqmi
qwqmimtgmtfgnizf
izfqwqmimtgmtfgn
mtfgnizfqwqmimtg
tgmtfgnizfqwqmim
mtfgnizfqwqmimtg
 2
bac
abc
 2
b
b
 2
dzlisvouhbqogzusikmkuvkql
qogzusikmkuvkqldzlisvouhc
 2
bac
cba
 2
acab
aabb
 2
aabb
acab
 2
nzxv
zvxn
 7
aaa
aab
aba
acb
baa
bab
bba
 2
njtazaab
azatjnba
 2
xyxx
xxyy
 4
aa
aa
bb
cb
 2
aabaab
baaaaa
 2
adam
adam
 2
abb
cba
 15
kknrrejishjz
hilbaftsfcaq
lncswtjqggjz
athvctulbhmb
nfvsjyiulmmr
hxjnvumwnwtr
rncsxqvkvqeg
qoabapuhodxk
ylinhbhyqjsn
cnzxgdgytgav
xufmacyangpv
hwvzionkdmjl
spklymjxiolk
qjkfrccaayak
onwrbgfvxrjx
 2
aaabc
baaba
 2
abdd
bdac
 2
aacc
acab
 10
ab
bc
cd
ed
ef
gh
hi
ij
ik
mn
 3
aaaa
aaab
aaaa
 2
abbc
ccba
 2
abcde
ebacd
 1
umwnrvtcytnqujdmqfiqt
 3
abcacc
bcabca
bcabca
 3
jwkb
kbkw
bkwk
 2
acc
acb
 2
abbabc
cabcab
 2
abc
aac
 2
ab
bb
 2
abcd
beca
 2
oiadfnwpdcxxhbwwqbrcdujcusgtkqdjmimtwjlb
brcdujcusgtkqdjmintwjlboiadfnwpdcxxhbwwq
 1
b
 2
sfotivvfgbdfcnvaybxhstavboktatflelpyi
sfotivvfgbdfcnvaybxhstavaoktatflelpyi
 3
baa
abb
bab
 3
jc
kc
kc
 2
mnlzv
lzvmo
 2
b
a
 2
dzlisvouhbqogzusikmkuvkql
chuovsilzdlqkvukmkisuzgoq
 2
abab
bbaa
 2
aabb
baca
 2
nzxv
nxvz
 7
aaa
aab
aba
acb
aba
bab
bba
 2
njtbzaab
azatjnba
 2
wyxx
xxyy
 2
aacaab
baaaaa
 2
mada
adam
 2
bba
cba
 15
kknrrejishjz
hilbaftsfcaq
lncswtjqggjz
athvctulbhmb
nfvsjyiulmmr
hxrnvumwnwtj
rncsxqvkvqeg
qoabapuhodxk
ylinhbhyqjsn
cnzxgdgytgav
xufmacyangpv
hwvzionkdmjl
spklymjxiolk
qjkfrccaayak
onwrbgfvxrjx
 2
aaabc
caaba
 2
aacb
acab
 10
ab
bc
cd
ed
eg
gh
hi
ij
ik
mn
 3
aaaa
aaab
`aaa
 2
abcc
ccba
 2
abcde
beacd
 1
umwnrvtcytnqukdmqfiqt
 3
jwkb
kbkw
bkwl
 2
acc
abb
 2
abbabc
cabcaa
 2
ba
bb
 2
abcd
bcea
 2
oiadfnwpdcxxhbwwqbrcdujcusgtkqdjmimtwjlb
brccujcusgtkqdjmintwjlboiadfnwpdcxxhbwwq
 1
`
 2
sfotivvfgbdfcnvaybxhstavboktatflelpyi
ifotsvvfgbdfcnvaybxhstavaoktatflelpyi
 3
baa
abb
bba
 2
cab
cba
 3
kc
kc
ck
 2
mnl{v
lzvmo
 2
c
a
 2
dzlisvouhbqogzusikmkuvkqk
chuovsilzdlqkvukmkisuzgoq
 2
abab
abba
 2
aabb
caca
 2
nzxv
nxuz
 7
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