#include<bits/stdc++.h>
using namespace std;
const int inf=999999999;
int Kralj=0;
int Lovac= 1;
int Top= 2;
int Konj= 3;
int Kraljica= 4;
int n,m;
const int KingX[8]={1,1,1,-1,-1,-1,0,0};
const int KingY[8]={0,1,-1,0,1,-1,1,-1};
const int HorseX[8]={-1,-1,1,1,-2,-2,2,2};
const int HorseY[8]={2,-2,2,-2,1,-1,1,-1};

const int QueenX[8]={1,1,1,-1,-1,-1,0,0};
const int QueenY[8]={0,1,-1,0,1,-1,1,-1};
const int CannonX[4]={0,0,-1,1};
const int CannonY[4]={1,-1,0,0};
const int BishopX[4]={1,1,-1,-1};
const int BishopY[4]={-1,1,-1,1};
void Check(vector <string> &a,int x=0,int y=0){
    if(x<0||y<0||x>=a.size()||y>a[0].size())return;
    if(a[x][y]!='.')return;
    a[x][y]='Z';
    for(int i=0;i<8;i++){
        Check(a,x+KingX[i],y+KingY[i]);
        Check(a,x+HorseX[i],y+HorseY[i]);
    }

}
void Die(int code){
    cout <<code<<endl;
    exit(0);
}
struct Move{
int x,y,Cena,Figure;
};
struct CompareMoves{
    bool operator()(Move const& A,Move  const&B){
        return A.Cena>B.Cena;
    }
};

vector <vector<int> > DP[5];
///vector <vector<bool> > Bio[5];
Move MakeMove(int xx,int yy,int f,int c){
Move m;
m.x=xx;
m.y=yy;
m.Cena=c;
m.Figure=f;
return m;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >>n>>m;

vector <string> a(n);


for(int i=0;i<n;i++)cin >>a[i];
 ///vector <string> cpy(n);
   /// cpy=a;
   /// cout <<"OH NO"<<endl;
  ///  Check(cpy);

    //if(cpy[n-1][m-1]!='Z')Die(-1);

for(int i=0;i<5;i++)DP[i].resize(n,vector <int> (m,inf));
///for(int i=0;i<5;i++)Bio[i].resize(n,vector <bool> (m));

priority_queue<Move,vector <Move>,CompareMoves > pq;

        pq.push(MakeMove(0,0,0,0));
        DP[0][0][0]=0;
    int solved=m*n*5;

    while(solved&&pq.size()){
        Move Current=pq.top();
        ///cout <<Current.Figure<<" "<<Current.x<<" "<<Current.y<<endl;
       /// cout <<pq.size()<<endl;

        pq.pop();
        if(Current.x<0||Current.y<0||Current.x>=n||Current.y>=m)continue;
        ///if(Bio[Current.Figure][Current.x][Current.y])continue;
        if(a[Current.x][Current.y]=='#')continue;
        ///Bio[Current.Figure][Current.x][Current.y]=true;
        //DP[Current.Figure][Current.x][Current.y]=Current.Cena;
        solved--;
        for(int i=0;i<5;i++){
            if(Current.Figure==i)continue;
            if(Current.Figure==Top&&i==Lovac)continue;
            if(Current.Figure==Lovac&&i==Top)continue;
            if(Current.Figure==Top&&i==Kraljica)continue;
            if(Current.Figure==Kraljica&&i==Top)continue;
            if(Current.Figure==Kraljica&&i==Lovac)continue;
            if(Current.Figure==Lovac&&i==Kraljica)continue;
            if(Current.Figure==Kraljica&&i==Kralj)continue;

           if(DP[i][Current.x][Current.y]>DP[Current.Figure][Current.x][Current.y]+i+1){
            DP[i][Current.x][Current.y]=DP[Current.Figure][Current.x][Current.y]+i+1;
            pq.push(MakeMove(Current.x,Current.y,i,DP[Current.Figure][Current.x][Current.y]+i+1));
           }

        }

        if(Current.Figure==Kralj){
            for(int i=0;i<8;i++){
                    if(Current.x+KingX[i]<0||Current.y+KingY[i]<0||Current.x+KingX[i]>=n||Current.y+KingY[i]>=m)continue;
                    if(a[Current.x+KingX[i]][Current.y+KingY[i]]=='#')continue;
             if(DP[Kralj][Current.x+KingX[i]][Current.y+KingY[i]]>DP[Current.Figure][Current.x][Current.y]+1){
                 DP[Kralj][Current.x+KingX[i]][Current.y+KingY[i]]=DP[Current.Figure][Current.x][Current.y]+1;
                pq.push(MakeMove(Current.x+KingX[i],Current.y+KingY[i],Kralj,DP[Current.Figure][Current.x][Current.y]+1));
             }
            }
        }

        if(Current.Figure==Konj){
             for(int i=0;i<8;i++){
            if(Current.x+HorseX[i]<0||Current.y+HorseY[i]<0||Current.x+HorseX[i]>=n||Current.y+HorseY[i]>=m)continue;
            if(a[Current.x+HorseX[i]][Current.y+HorseY[i]]=='#')continue;
             if(DP[Konj][Current.x+HorseX[i]][Current.y+HorseY[i]]>DP[Current.Figure][Current.x][Current.y]+1){
                 DP[Konj][Current.x+HorseX[i]][Current.y+HorseY[i]]=DP[Current.Figure][Current.x][Current.y]+1;
                pq.push(MakeMove(Current.x+HorseX[i],Current.y+HorseY[i],Konj,DP[Current.Figure][Current.x][Current.y]+1));
             }
            }
        }

        if(Current.Figure==Kraljica){
            for(int i=0;i<8;i++){
                int X=Current.x+QueenX[i];
                int Y=Current.y+QueenY[i];
                while(X>=0&&X<n&&Y>=0&&Y<m&&a[X][Y]!='#'){
                    if(DP[Kraljica][X][Y]>DP[Current.Figure][Current.x][Current.y]+1){
                            DP[Kraljica][X][Y]=DP[Current.Figure][Current.x][Current.y]+1;
                       pq.push(MakeMove(X,Y,Kraljica,DP[Current.Figure][Current.x][Current.y]+1));
                    }


                    X+=QueenX[i];
                    Y+=QueenY[i];


                }
            }
        }

        if(Current.Figure==Lovac){
            for(int i=0;i<4;i++){
               int X=Current.x+BishopX[i];
                int Y=Current.y+BishopY[i];
                while(X>=0&&X<n&&Y>=0&&Y<m&&a[X][Y]!='#'){
                    if(DP[Lovac][X][Y]>DP[Current.Figure][Current.x][Current.y]+1){
                            DP[Lovac][X][Y]=DP[Current.Figure][Current.x][Current.y]+1;
                       pq.push(MakeMove(X,Y,Lovac,DP[Current.Figure][Current.x][Current.y]+1));
                    }


                    X+=BishopX[i];
                    Y+=BishopY[i];

            }
        }
        }

        if(Current.Figure==Top){
            for(int i=0;i<4;i++){
                int X=Current.x+CannonX[i];
                int Y=Current.y+CannonY[i];
                while(X>=0&&X<n&&Y>=0&&Y<m&&a[X][Y]!='#'){
                    if(DP[Top][X][Y]>DP[Current.Figure][Current.x][Current.y]+1){
                            DP[Top][X][Y]=DP[Current.Figure][Current.x][Current.y]+1;
                       pq.push(MakeMove(X,Y,Top,DP[Current.Figure][Current.x][Current.y]+1));
                    }


                    X+=CannonX[i];
                    Y+=CannonY[i];
                }
            }
        }


    }/*
 for(int k=0;k<5;k++){
        cout <<endl<<k<<endl;
 cout <<"_____________________________________________________________"<<endl;
     for(int i=0;i<n;i++){
           cout <<endl;
        for(int j=0;j<m;j++){
            cout <<DP[k][i][j] <<" ";
        }
     }
 }*/
        int rez=DP[0][n-1][m-1];
    int fig=0;
    for(int i=0;i<5;i++){
        if(rez>DP[i][n-1][m-1])fig=i;
    rez=min(rez,DP[i][n-1][m-1]);
    }
   /// cout <<"FIG: "<<fig<<endl;
   if(rez==inf)Die(-1);
    cout <<rez<<endl;

}