#include <iostream>
#include <climits>
#include <vector>
#include <cstring>
using namespace std;

// 0 = desno
// 1 = dole
// 2 = dijagonalno,desno dole
// 2 = konjski
// 3 = promena

// 0 = slobodno
// 1 = no



int najmanja(vector<vector<int>> mapa, int loc[2], string figura, bool promena) {
    int pomerajmin = INT_MAX/2;
    //cout << "activated, figura: " << figura << " promena: (boolean) " << to_string(promena) << endl;
    //cout << "KOORDINATE: " << loc[0] << " " << loc[1] << endl;
    for (int i = 0; i <=3; i++) {
            //cout << "i is: " << i;
            //cout << "   koordinate: " << loc[0] << " " << loc[1] << endl;
        if (i == 0 && (figura == "kraljica" || figura == "top" || figura == "kralj") && loc[0]+1 != mapa.size()) {
            //cout << "prvi ciklus" << endl;
            int locn[2];
            memcpy(locn,loc,sizeof(locn));
            int pomeraj = 0;
            if (mapa[loc[0]+1][loc[1]] == 0) {
                if (figura== "kralj") {
                    locn[0]++;
                    //pomeraj++;
                } else {
                    for (int g = loc[0]; g < mapa.size()-1; g++) {
                        if (mapa[locn[0]+1][loc[1]] == 0) {
                            locn[0]++;
                            //pomeraj++;
                        } else {
                            break;
                        }
                    }
                }
                pomeraj++;

            if (locn[0] == mapa.size() -1 && locn[1] == mapa[0].size() -1) {
                //cout << "YEET!" << endl;
                pomerajmin = pomeraj;
                break;
            } else {
                //cout << "0 lokacija je sada: " << locn[0] << " " << locn[1] << endl;
                pomerajmin = najmanja(mapa, locn, figura, false) + pomeraj;
                //cout << "pomerajmin je " << pomerajmin << endl;
                //cout << "dalje0" << endl;
            }
            }
        }
        //if (i==1) {
        //    cout << "sada bi se trebao desiti drugi ciklus " <<endl;
        //}
        else if ((i == 1) && ((figura == "kraljica") || (figura == "kralj") || (figura == "top")) && loc[1] +1 != mapa[0].size()) {
            //cout << "loc[1] je: " << loc[1] << ", a mapa[0].size(): " << mapa[0].size() << endl;
            //cout << "drugi ciklus" << endl;
            int locn[2];
            memcpy(locn,loc,sizeof(locn));
            int pomeraj = 0;
            if (mapa[loc[0]][loc[1]+1] == 0) {
                if (figura== "kralj") {
                        //cout << "locn[1] se povecava!" << endl;
                    locn[1]++;
                    //pomeraj++;
                } else {
                    for (int g = loc[1]; g < mapa[0].size()-1; g++) {
                        if (mapa[locn[0]][locn[1]+1] == 0) {
                                //cout << "locn[1] se povecava!" << endl;
                            locn[1]++;
                            //pomeraj++;
                        } else {
                            break;
                        }
                    }
                }
                pomeraj++;
            if (locn[0] == mapa.size() -1 && locn[1] == mapa[0].size() -1) {
                    //cout << "2: yeet, na cilju smo, pomeraj: " << pomeraj << endl;
                pomerajmin = pomeraj;
                break;
            } else {
                //cout << "2: ima jos do cilja" << endl;
                //cout << "locn[1] je " << locn[1] << endl;
                int minpom = najmanja(mapa,locn, figura, false) + pomeraj;
                //cout << "1 lokacija je sada: " << locn[0] << " " << locn[1] << endl;
                //cout << "pomeraj je ovde: " << minpom << " a loc: " << loc[0] << " " << loc[1] << endl;
                if (minpom < pomerajmin) {pomerajmin = minpom;}
            }
            }
        }
        else if (i == 2 && (figura != "top") && loc[0] +1 != mapa.size() && loc[1] + 1 != mapa[0].size()) {
            int locn[2];
            bool brake = false;
            memcpy(locn,loc,sizeof(locn));
            int pomeraj = 0;
            if (figura != "konj") {
                if (mapa[locn[0]+1][locn[1]+1] == 0) {
                    pomeraj++;
                while (locn[0] < mapa.size() -1 && locn[1] < mapa[0].size()-1) {
                    if (mapa[locn[0]+1][locn[1]+1] == 0) {
                        locn[0]++;
                        locn[1]++;
                    } else {break;}
                    if (figura == "kralj") {break;}
                }
                //cout <<  "racunanje minpom stvari " << endl;
                int minpom = najmanja(mapa,locn, figura, false) + pomeraj;
                if (minpom < pomerajmin) {pomerajmin = minpom;brake = true;}
                //cout << "2 lokacija je sada: " << locn[0] << " " << locn[1] << endl;
                //cout << "takodje pomeraj dijagonalnog sranja je: " << pomeraj << " a minpom je: " << minpom << endl;
                }
            } else {
                if ((loc[1] + 2 != mapa[0].size()) && mapa[locn[0]+1][locn[1]+2] == 0) {
                    locn[0]+=1;
                    locn[1]+=2;
                    if (locn[0] == mapa.size()-1 && locn[1] == mapa[0].size()-1) {
                        pomerajmin = 1;
                        //cout << "za i = 2 stiglo smo na cilj!" << endl;
                        brake = true;
                    } else {
                        int minpom = najmanja(mapa,locn, figura, false) + 1;
                        if (minpom < pomerajmin) {pomerajmin = minpom;brake = true;}
                        //cout << "za i = 2 minpom je " << minpom << endl;
                    }
                }
                if (brake) {break;}
                memcpy(locn,loc,sizeof(locn));
                if ((loc[0]+2 < mapa.size())
                    && mapa[locn[0]+2]
                    [locn[1]+1]
                    == 0
                    ) {
                    locn[0]+=2;
                    locn[1]+=1;
                    //cout << "3k: ovim se stize kao na cilj" << endl;
                    if (locn[0] == mapa.size() && locn[1] == mapa[0].size()) {
                        pomerajmin = pomeraj;
                        brake = true;
                    } else {
                        //cout << "3k: ima jos do cilja" << endl;
                        int minpom = najmanja(mapa,locn, figura, false) + 1;
                        if (minpom < pomerajmin) {minpom = pomerajmin;brake = true;}
                    }
                }
            }
            if (brake) {break;}
        }
        else if (i==3) {
                //cout << "4 ciklus!" << endl;
            if (!promena) {
                //cout << "4 ciklus pass!" << endl;
                if (figura != "kraljica") {
                    int promenak = 5 + najmanja(mapa,loc, "kraljica", true);
                    if (promenak < pomerajmin) {
                        pomerajmin = promenak;
                    }
                }
                if (figura != "top") {
                    int promenat = 3 + najmanja(mapa,loc,"top", true);
                    if (promenat < pomerajmin) {
                        pomerajmin = promenat;
                    }
                }
                if (figura != "konj") {
                    int promenat = 4 + najmanja(mapa,loc,"konj", true);
                    if (promenat < pomerajmin) {
                        pomerajmin = promenat;
                    }
                }
                if (figura != "lovac") {
                    int promenat = 2 + najmanja(mapa,loc,"lovac", true);
                    if (promenat < pomerajmin) {
                        pomerajmin = promenat;
                    }
                }
                if (figura != "kralj") {
                    int promenat = 1 + najmanja(mapa,loc,"kralj", true);
                    if (promenat < pomerajmin) {
                        pomerajmin = promenat;
                    }
                }
            }
        }
    }
    //cout << "ENDIRNATE: " << loc[0] << " " << loc[1] << " <> a pomerajmin je: " << pomerajmin << endl;
    return pomerajmin;
}

int main()
{
    int h,w;
    cin >> h >> w;
    vector<vector<int>> mapa;
    for (int i = 0; i < h; i++) {
        vector<int> mapica;
        string r;
        cin >> r;
        for (int g = 0; g < w; g++) {
            int o;
            if (r.at(g) == '.') {o = 0;}
            else {o = 1;}
            mapica.push_back(o);
        }
        mapa.push_back(mapica);
    }
    //cout << "x mape je: " << mapa.size() << ", a y: " << mapa[0].size() << endl;
    //cout << "done" <<endl;
    int loc[2] = {0,0};
    cout << najmanja(mapa,loc,"kralj", false) << endl;
    return 0;
}