#include <bits/stdc++.h>
namespace sash {
using namespace std;
template <class T>
using v = vector<T>;
template <class T>
using vv = vector<v<T>>;
template <class T>
using pri = priority_queue<T>;
template <class T>
using rpi = priority_queue<T, v<T>, greater<T>>;
using vvb = v<vector<bool>>;
using vs = v<string>;
using vvi = v<vector<long long>>;
using vc = v<char>;
template <class T>
inline void rd(T& a) {
  cin >> a;
}
template <class T>
inline void rd(v<T>& a, long long n) {
  T x;
  for (long long i = (long long)0; i < (long long)n; i++) {
    cin >> x;
    a[i] = x;
  }
}
template <class T>
inline void rd(vv<T>& a, long long n, long long m) {
  for (long long i = (long long)0; i < (long long)n; i++) {
    rd(a[i], m);
  }
}
template <class T, class... Args>
inline void rd(T& a, Args&... args) {
  cin >> a;
  rd(args...);
}
template <class T>
inline void print(T a) {
  cout << (a) << '\n';
}
template <class T>
inline void print(v<T> a) {
  for (auto x : a) cout << (x) << ' ';
  cout << '\n';
}
template <class T>
inline void print(vv<T> a) {
  for (auto x : a) print(x);
}
inline long long mintwo(long long n) { return __builtin_ctzll(n); }
inline long long maxtwo(long long n) { return (long long)log2(n); }
inline long long onebits(long long n) { return __builtin_popcountll(n); }
inline long double pdiv(long long a, long long b) {
  return (1.0 * (a)) / (1.0 * (b));
}
inline void chxor(long long& a, long long b) { a = a ^ b; }
inline void mangekyo();
inline void subl() {}
long double _c;
using i32 = int32_t;
template <class T>
inline long long sum(v<T> a) {
  long long s = 0;
  for (auto x : a) s += x;
  return s;
}
template <class T>
inline T min(v<T> a) {
  return *min_element(a.begin(), a.end());
}
template <class T>
inline T max(v<T> a) {
  return *max_element(a.begin(), a.end());
}
template <class T, class U>
inline void chmax(T& a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void chmin(T& a, U b) {
  if (a > b) a = b;
}
template <class T, class U>
inline T max(T a, U b) {
  return a > b ? a : b;
}
template <class T, class U>
inline T min(T a, U b) {
  return a < b ? a : b;
}
template <class T>
long long POW(T& a, long long b) {
  long long r = 1, p = a;
  for (long long e = b; e > 0; e >>= 1, p *= p) r *= (e & 1) ? p : 1;
  return r;
}
template <class T>
v<T> pre_sum(v<T> a) {
  for (long long i = (long long)1; i < (long long)a.size(); i++)
    a[i] += a[i - 1];
  return a;
}
template <class T>
v<T> suf_sum(v<T> a) {
  for (long long i = (long long)a.size() - 1 - 1; i > (long long)0 - 1; i--)
    a[i] += a[i + 1];
  return a;
}
void factfill(vector<long long>& fact, long long n) {
  fact.resize(n + 1, 1);
  for (long long i = (long long)1; i < (long long)n + 1; i++)
    fact[i] = fact[i - 1] * i;
}
const long long inf = 1e9, mod = 1e9 + 7;
}  // namespace sash
using namespace sash;
void solve() {
  long long dp[512][512];
  string store[512];
  long long n;
  rd(n);
  for (long long i = (long long)1; i < (long long)n + 1; i++) rd(store[i]);
  long long k = store[1].size(), ans = inf;
  for (long long i = (long long)1; i < (long long)n + 1; i++)
    store[i] = store[i] + store[i];
  for (long long i = (long long)1; i < (long long)n + 1; i++)
    for (long long j = (long long)0; j < (long long)k; j++) dp[i][j] = inf;
  for (long long i = (long long)0; i < (long long)k; i++) dp[1][i] = i;
  for (long long i = (long long)2; i < (long long)n + 1; i++) {
    for (long long j = (long long)0; j < (long long)k; j++) {
      for (long long prv = (long long)0; prv < (long long)k; prv++) {
        if (store[i].substr(j, k) == store[i - 1].substr(prv, k))
          chmin(dp[i][j], dp[i - 1][prv] + j);
      }
    }
  }
  for (long long i = (long long)0; i < (long long)k; i++) chmin(ans, dp[n][i]);
  print(ans == inf ? -1 : ans);
}
i32 main() {
  mangekyo();
  solve();
  cerr << "Time: " << ((clock() - _c) / CLOCKS_PER_SEC);
  return 0;
}
void sash::mangekyo() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  srand(time(NULL));
  _c = clock();
}
