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

Copy
4
xzzwo
zwoxz
zzwox
xzzwo
output

Copy
5
input

Copy
2
molzv
lzvmo
output

Copy
2
input

Copy
3
kc
kc
kc
output

Copy
0
input

Copy
3
aa
aa
ab
output

Copy
-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".


数据量小,暴力解决

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const int maxn = ;
const long long inf = 0x7f7f7f7f7f7f7f7f; int main()
{
int n;
cin >> n;
vector<string> s(n);
for (int i = ; i < n; i++)
cin >> s[i];
int len = s[].size();
int dp=1e9;
for (int i = ; i < n; i++)
{
int cnt = ;
for (int j = ; j < n; j++)
{
string t=s[j];
if (j != i)
{
for (int k = ; k < len; k++)
{
if (k == len - && t != s[i])
{
cout << -;
return ;
}
if (t == s[i])
break;
else
{
char first=t[];
t.erase(,);
t += first;
cnt++;
}
}
}
}
dp = min(dp, cnt);
}
cout << dp;
return ;
}

最新文章

  1. spring JPA 动态查询
  2. toolControls添加工具项
  3. devexpress13学习系列(一)PDFViewer(1)
  4. jsp网站与discuz论坛用户同步
  5. Ubuntu Server 14.04在VMware安装的一些事儿
  6. .net面试题【持续更新.....】
  7. 201521123011 《Java程序设计》第4周学习总结
  8. ibm云时代的转型
  9. vue+axios访问本地json数据踩坑点
  10. gitLib操作笔录《一》:创建分支,切换分支,提交分支到远程,以及基本代码clone与更新提交到远程操作指令
  11. Python-递归初识-50
  12. maya2014卸载/安装失败/如何彻底卸载清除干净maya2014注册表和文件的方法
  13. xml中的四则运算与时间爱格式
  14. 【译】第15节---数据注解-StringLength
  15. Redis zset数据类型
  16. Git之远程库与本地库交互
  17. Android 笔记-Fragment 与 Activity之间传递数据
  18. oracle 快速备份表数据
  19. 有限制的最短路spfa+优先队列
  20. LeetCode OJ:Linked List Cycle II(循环链表II)

热门文章

  1. 【第一篇】为什么选择xLua
  2. Go String
  3. HTML(多行)文本超过部分隐藏,末尾显示(...)
  4. .net Core3.0 +Nlog+Sqlserver
  5. day 8 函数编程_byets
  6. CentOS7安装docker和docker-compose
  7. Java自学-Lambda 方法引用
  8. 《The good doctor》视频笔记
  9. SpringBoot整合ActiveMQ开启持久化
  10. php 常用数学函数