using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 4");
WillReturn.Add("..a.");
WillReturn.Add("####");
WillReturn.Add("ba#b");
//5
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 4");
WillReturn.Add("..a.");
WillReturn.Add("####");
WillReturn.Add("b.#b");
//-1
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 4");
WillReturn.Add("xxxx");
WillReturn.Add("xxxx");
WillReturn.Add("xxxx");
WillReturn.Add("xxxx");
//1
}
else if (InputPattern == "Input4") {
WillReturn.Add("7 11");
WillReturn.Add("u..#y..#...");
WillReturn.Add("k..#.z.#.k.");
WillReturn.Add("iju#...#x..");
WillReturn.Add("###########");
WillReturn.Add("..x#.t.#..n");
WillReturn.Add("abc#y..#...");
WillReturn.Add("..z#..t#.y.");
//12
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
static void Main()
{
List<string> InputList = GetInputList();
char[,] BanArr = CreateBanArr(InputList.Skip(1));
int UB_X = BanArr.GetUpperBound(0);
int UB_Y = BanArr.GetUpperBound(1);
// 座標のList[文字]なDict
var PosListDict = new Dictionary<char, List<PointDef>>();
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
char CurrChar = BanArr[X, Y];
if ('a' <= CurrChar && CurrChar <= 'z') {
if (PosListDict.ContainsKey(CurrChar) == false) {
PosListDict[CurrChar] = new List<PointDef>();
}
PointDef WillAdd;
WillAdd.X = X;
WillAdd.Y = Y;
PosListDict[CurrChar].Add(WillAdd);
}
}
}
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrX = 0;
WillEnqueue.CurrY = 0;
WillEnqueue.Level = 0;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<int>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
// クリア判定
if (Dequeued.CurrX == UB_X && Dequeued.CurrY == UB_Y) {
Console.WriteLine(Dequeued.Level);
return;
}
// ワープで移動
char CurrChar = BanArr[Dequeued.CurrX, Dequeued.CurrY];
if (PosListDict.ContainsKey(CurrChar)) {
foreach (PointDef EachPos in PosListDict[CurrChar]) {
WillEnqueue.CurrX = EachPos.X;
WillEnqueue.CurrY = EachPos.Y;
WillEnqueue.Level = Dequeued.Level + 1;
int Hash = GetHash(EachPos.X, EachPos.Y);
if (VisitedSet.Add(Hash)) {
Que.Enqueue(WillEnqueue);
}
}
// 今後はワープ不可
PosListDict.Remove(CurrChar);
}
// 4近傍に移動
Action<int, int> MoveAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB_X < pNewX) return;
if (pNewY < 0 || UB_Y < pNewY) return;
if (BanArr[pNewX, pNewY] == '#') return;
WillEnqueue.CurrX = pNewX;
WillEnqueue.CurrY = pNewY;
WillEnqueue.Level = Dequeued.Level + 1;
int Hash = GetHash(pNewX, pNewY);
if (VisitedSet.Add(Hash)) {
Que.Enqueue(WillEnqueue);
}
};
MoveAct(Dequeued.CurrX, Dequeued.CurrY - 1);
MoveAct(Dequeued.CurrX, Dequeued.CurrY + 1);
MoveAct(Dequeued.CurrX - 1, Dequeued.CurrY);
MoveAct(Dequeued.CurrX + 1, Dequeued.CurrY);
}
Console.WriteLine(-1);
}
static int GetHash(int pX, int pY)
{
int Hash = 0;
Hash += pX;
Hash *= 10000;
Hash += pY;
return Hash;
}
struct JyoutaiDef
{
internal int CurrX;
internal int CurrY;
internal int Level;
}
struct PointDef
{
internal int X;
internal int Y;
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をcharの2次元配列に設定
////////////////////////////////////////////////////////////////
static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new char[0, 0];
}
int UB_X = StrList[0].Length - 1;
int UB_Y = StrList.Count - 1;
char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = StrList[Y][X];
}
}
return WillReturn;
}
}