#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int ans = 100000000;
int n;
char arr[110][100];
bool vis[110][110];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void dfs(int x, int y, int cnt,char c)
{
if (arr[x][y] == 'B')
{
if (cnt < ans)
{
ans = cnt;
}
return;
}
for (int i = 0;i < 4;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 1 && yy >= 1 && xx <= n && yy <= n && vis[xx][yy] && arr[xx][yy] != arr[x][y] && cnt + 1 < ans)
{
vis[xx][yy] = 0;
dfs(xx, yy, cnt + 1, arr[xx][yy]);
vis[xx][yy] = 1;
}
}
}
int main()
{
cin >> n;
int a, b;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> arr[i][j];
if (arr[i][j] == 'A')
{
a = i;
b = j;
}
}
}
memset(vis, 1, sizeof(vis));
vis[a][b] = 0;
dfs(a, b, 0, 'q');
if (ans == 100000000)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
#include<string>
int arr[30];
int main()
{
string str;
int k;
cin >> k;
cin >> str;
int cnt = 0;
int l = str.length();
if (l % k != 0)
{
cout << -1 << endl;
return 0;
}
else
{
int n = l / k;
for (int i = 0;i < n;i++)
{
memset(arr, 0, sizeof(arr));
for (int j = 0;j < k;j++)
{
char c = str[i + j * n];
int x = int(c - 96);
arr[x]++;
}
int u = 0;
for (int i = 1;i <= 27;i++)
{
if (arr[i] > u)
{
u = arr[i];
}
}
cnt += k - u;
}
}
cout << cnt << endl;
return 0;
}