#include <bits/stdc++.h>
using namespace std;
void Input_Preparing() {}
int aws = 0;
vector<string> a, b;
int n, q;
map<string, bool> dp;
vector<bool> Used(40);
bool Check(string s) {
  if (s == "a") return true;
  if (dp[s]) return true;
  for (int i = 1; i <= q; ++i) {
    if (a[i] == s.substr(0, 2)) {
      Used[i] = 1;
      dp[s] = Check(b[i] + s.substr(2));
      Used[i] = 0;
    }
    if (dp[s]) return true;
  }
  return dp[s];
}
string s = "";
void DFS(int index) {
  if (index > n) {
    if (Check(s)) ++aws;
  } else {
    for (int i = 'a'; i <= 'f'; ++i) {
      if (s.size() < index)
        s.push_back((char)i);
      else
        s[index - 1] = (char)i;
      DFS(index + 1);
    }
  }
}
void Processing() {
  int n;
  cin >> n;
  vector<string> s(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
  }
  vector<vector<int> > dp(512, vector<int>(512, 1e9));
  int k = s[1].length();
  for (int i = 1; i <= n; ++i) s[i] = s[i] + s[i];
  for (int i = 0; i <= k - 1; ++i) {
    dp[1][i] = i;
  }
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j <= k - 1; ++j) {
      for (int prev = 0; prev <= k - 1; ++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 aws = 1e9;
  for (int i = 0; i <= k - 1; ++i) aws = min(aws, dp[n][i]);
  cout << (aws == 1e9 ? -1 : aws);
}
int main() {
  std::cin.tie(0)->sync_with_stdio(0);
  ;
  Processing();
  return 0;
}
