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("1S23");
WillReturn.Add("4567");
WillReturn.Add("89G1");
//17
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 11");
WillReturn.Add("S134258976G");
//20
}
else if (InputPattern == "Input3") {
WillReturn.Add("3 3");
WillReturn.Add("S12");
WillReturn.Add("4G7");
WillReturn.Add("593");
//-1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct PointDef
{
internal int X;
internal int Y;
}
struct JyoutaiDef
{
internal int CurrNum;
internal int CurrX;
internal int CurrY;
internal int SumCost;
}
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);
// Sを0、Gを10として、座標のList[数値]なDict
var NumPosListDict = new Dictionary<int, List<PointDef>>();
for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
int CurrNum;
if (BanArr[LoopX, LoopY] == 'S') CurrNum = 0;
else if (BanArr[LoopX, LoopY] == 'G') CurrNum = 10;
else CurrNum = BanArr[LoopX, LoopY] - '0';
if (NumPosListDict.ContainsKey(CurrNum) == false) {
NumPosListDict[CurrNum] = new List<PointDef>();
}
NumPosListDict[CurrNum].Add(new PointDef() { X = LoopX, Y = LoopY });
}
}
int StaX = NumPosListDict[0][0].X;
int StaY = NumPosListDict[0][0].Y;
int GoalX = NumPosListDict[10][0].X;
int GoalY = NumPosListDict[10][0].Y;
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrNum = 0;
WillPush.CurrX = StaX;
WillPush.CurrY = StaY;
WillPush.SumCost = 0;
Stk.Push(WillPush);
// 最小コスト[現在座標のハッシュ値]なDict
var MinCostDict = new Dictionary<int, int>();
MinCostDict[GetHash(StaX, StaY)] = 0;
bool FoundAnswer = false;
int Answer = int.MaxValue;
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
// クリア判定
if (Popped.CurrNum == 10) {
FoundAnswer = true;
Answer = Popped.SumCost;
continue;
}
int NextNum = Popped.CurrNum + 1;
if (NumPosListDict.ContainsKey(NextNum) == false) {
continue;
}
foreach (PointDef EachNextPoint in NumPosListDict[NextNum]) {
WillPush.CurrNum = NextNum;
WillPush.CurrX = EachNextPoint.X;
WillPush.CurrY = EachNextPoint.Y;
int Manhattan = DeriveManhattan(Popped.CurrX, Popped.CurrY,
WillPush.CurrX, WillPush.CurrY);
WillPush.SumCost = Popped.SumCost + Manhattan;
int Hash = GetHash(WillPush.CurrX, WillPush.CurrY);
if (MinCostDict.ContainsKey(Hash)) {
if (MinCostDict[Hash] <= WillPush.SumCost) {
continue;
}
}
MinCostDict[Hash] = WillPush.SumCost;
Stk.Push(WillPush);
}
}
if (FoundAnswer) Console.WriteLine(Answer);
else Console.WriteLine(-1);
}
// 座標のハッシュ値を返す
static int GetHash(int pX, int pY)
{
return pX * 100 + pY;
}
// 2つの座標のマンハッタン距離を返す
static int DeriveManhattan(int pX1, int pY1, int pX2, int pY2)
{
int WillReturn = 0;
WillReturn += Math.Abs(pX1 - pX2);
WillReturn += Math.Abs(pY1 - pY2);
return WillReturn;
}
////////////////////////////////////////////////////////////////
// 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;
}
}