# include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
string s[512];
int dp[512][512];
int main(void)
{
int n;
fi>>n;
for (int i = 1;i <= n;++i)
fi>>s[i];
int k = s[1].length();
for (int i = 1;i <= n;++i)
for (int j = 0;j < k;++j)
dp[i][j] = 1e9;
for (int i = 1;i <= n;++i)
s[i] = s[i] + s[i];
for (int i = 0;i < k;++i)
dp[1][i] = i;
for (int i = 2;i <= n;++i)
{
for (int j = 0;j < k;++j)
for (int prev = 0;prev < k;++prev)
if (s[i].substr(j,k) == s[i-1].substr(prev,k))
dp[i][j] = min(dp[i][j],dp[i-1][prev] + j);
}
int ans = 1e9;
for (int i = 0;i < k;++i)
ans = min(ans,dp[n][i]);
if (ans == 1e9)
puts("-1");
else
fo << ans << '\n';
return 0;
}
IyBpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojIGRlZmluZSBmaSBjaW4KIyBkZWZpbmUgZm8gY291dApzdHJpbmcgc1s1MTJdOwppbnQgZHBbNTEyXVs1MTJdOwppbnQgbWFpbih2b2lkKQp7CiAgICBpbnQgbjsKICAgIGZpPj5uOwogICAgZm9yIChpbnQgaSA9IDE7aSA8PSBuOysraSkKICAgICAgICBmaT4+c1tpXTsKICAgIGludCBrID0gc1sxXS5sZW5ndGgoKTsKICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjsrK2kpCiAgICAgICAgZm9yIChpbnQgaiA9IDA7aiA8IGs7KytqKQogICAgICAgICAgICBkcFtpXVtqXSA9IDFlOTsKICAgIGZvciAoaW50IGkgPSAxO2kgPD0gbjsrK2kpCiAgICAgICAgc1tpXSA9IHNbaV0gKyBzW2ldOwogICAgZm9yIChpbnQgaSA9IDA7aSA8IGs7KytpKQogICAgICAgIGRwWzFdW2ldID0gaTsKICAgIGZvciAoaW50IGkgPSAyO2kgPD0gbjsrK2kpCiAgICB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7aiA8IGs7KytqKQogICAgICAgICAgICBmb3IgKGludCBwcmV2ID0gMDtwcmV2IDwgazsrK3ByZXYpCiAgICAgICAgICAgICAgICBpZiAoc1tpXS5zdWJzdHIoaixrKSA9PSBzW2ktMV0uc3Vic3RyKHByZXYsaykpCiAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBtaW4oZHBbaV1bal0sZHBbaS0xXVtwcmV2XSArIGopOwogICAgfQogICAgaW50IGFucyA9IDFlOTsKICAgIGZvciAoaW50IGkgPSAwO2kgPCBrOysraSkKICAgICAgICBhbnMgPSBtaW4oYW5zLGRwW25dW2ldKTsKICAgIGlmIChhbnMgPT0gMWU5KQogICAgICAgIHB1dHMoIi0xIik7CiAgICBlbHNlCiAgICAgICAgZm8gPDwgYW5zIDw8ICdcbic7CiAgICByZXR1cm4gMDsKfQoKCg==