#include <bits/stdc++.h>
#define MAX_POTEZ 2147000000

using namespace std;


struct Tkord {int k,b,r,ki,q; short int x,y;};
int h,w;
vector<vector<char> > s;
queue<Tkord> redZaObradu;
Tkord p={MAX_POTEZ,MAX_POTEZ,MAX_POTEZ,MAX_POTEZ,MAX_POTEZ,-1,-1};

vector<vector<Tkord> > potezi;
int smer1[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int smer2[4][2]={{1,1},{-1,-1},{1,-1},{-1,1}};
int konj[8][2]={{1,2},{-1,-2},{-1,2},{1,-2},{2,1},{-2,-1},{-2,1},{2,-1}};
int bfs()
{
    while(!redZaObradu.empty()){
        Tkord trenutni=redZaObradu.front();
        int x=trenutni.x,y=trenutni.y;

        int quik=min(potezi[x][y].k,min(potezi[x][y].b,min(potezi[x][y].r,min(potezi[x][y].ki,potezi[x][y].q))));

        potezi[x][y].k=min(quik+1,potezi[x][y].k);
        potezi[x][y].b=min(quik+2,potezi[x][y].b);
        potezi[x][y].r=min(quik+3,potezi[x][y].r);
        potezi[x][y].ki=min(quik+4,potezi[x][y].ki);
        potezi[x][y].q=min(quik+5,potezi[x][y].q);

        for(int i=0;i<4;i++){
            int k=x+smer1[i][0],l=y+smer1[i][1];
            while(s[k][l]!='#'){
                bool obradiPolje=false;
                if(k==x+smer1[i][0]&&l==y+smer1[i][1]){
                    if(potezi[k][l].k>potezi[x][y].k+1){
                        potezi[k][l].k=potezi[x][y].k+1;
                        obradiPolje=1;
                    }
                }
                if(potezi[k][l].r>potezi[x][y].r+1){
                    potezi[k][l].r=potezi[x][y].r+1;
                    obradiPolje=1;
                }
                if(potezi[k][l].q>potezi[x][y].q+1){
                    potezi[k][l].q=potezi[x][y].q+1;
                    obradiPolje=1;
                }
                if(obradiPolje&&k!=h+1&&l!=w+1){p={0,0,0,0,0,k,l}; redZaObradu.push(p);}
                k+=smer1[i][0];
                l+=smer1[i][1];

            }
        }

        for(int i=0;i<4;i++){
            int k=x+smer2[i][0],l=y+smer2[i][1];
            while(s[k][l]!='#'){
                bool obradiPolje=false;
                if(k==x+smer1[i][0]&&l==y+smer1[i][1]){
                    if(potezi[k][l].k>potezi[x][y].k+1){
                        potezi[k][l].k=potezi[x][y].k+1;
                        obradiPolje=1;
                    }
                }
                if(potezi[k][l].b>potezi[x][y].b+1){
                    potezi[k][l].b=potezi[x][y].b+1;
                    obradiPolje=1;
                }
                if(potezi[k][l].q>potezi[x][y].q+1){
                    potezi[k][l].q=potezi[x][y].q+1;
                    obradiPolje=1;
                }
                if(obradiPolje&&k!=h+1&&l!=w+1){p={0,0,0,0,0,k,l}; redZaObradu.push(p);}
                k+=smer1[i][0];
                l+=smer1[i][1];
            }
        }

        for(int i=0;i<8;i++){
            int k=x+smer2[i][0],l=y+smer2[i][1];

            bool obradiPolje=false;
            if(potezi[k][l].ki>potezi[x][y].ki+1&&s[k][l]!='#'){
                potezi[k][l].ki=potezi[x][y].ki+1;
                obradiPolje=1;
            }
            if(obradiPolje&&k!=h+1&&l!=w+1){p={0,0,0,0,0,k,l}; redZaObradu.push(p);}

        }


        redZaObradu.pop();
    }
    return min(potezi[h+1][w+1].k,min(potezi[h+1][w+1].b,min(potezi[h+1][w+1].r,min(potezi[h+1][w+1].ki,potezi[h+1][w+1].q))));

}



int main()
{

    cin>>h>>w;
    s= vector<vector<char> > (h+4,vector<char> (w+4,'#'));
    potezi=vector<vector<Tkord> > (h+4,vector<Tkord> (w+4,p));
    for(int i=2;i<h+2;i++){
        for(int j=2;j<w+2;j++){
            cin>> s[i][j];

        }
    }

    p={0,2,3,4,5,2,2};
    potezi[2][2]=p;
    redZaObradu.push (p);
    int res=bfs();
    if(res==MAX_POTEZ)
        cout << -1 << endl;
    else
        cout << res <<endl;
    return 0;
}