#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0;i<(int)(n);i++)
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
int main(){
    int R,C;
    cin >> R >> C;
    vector<vector<char>> table(R, vector<char>(C));
    vector<vector<int>> dist(R, vector<int>(C));
    int sy, sx, gy, gx;
    vector<int> alx[26], aly[26];
    bool alf[26]={};
    rep(i, R){
        rep(j, C){
            cin >> table.at(i).at(j);
            if(table.at(i).at(j) == 'S'){sx = j; sy = i;}
            if(table.at(i).at(j) == 'G'){gx = j; gy = i;}
            if(table.at(i).at(j) >= 'a' && table.at(i).at(j) <= 'z'){
                assert(table.at(i).at(j) - 'a' < 26);
                alx[table.at(i).at(j) - 'a'].push_back(j);
                aly[table.at(i).at(j) - 'a'].push_back(i);
                }
            dist.at(i).at(j) = INF;
        }
    }
  //sx--; sy--; gx--; gy--;
    queue<pair<int, int>> Q;
    Q.push(make_pair(sy, sx));
    dist.at(sy).at(sx)=0;
    
    while(!Q.empty()){
        int cy = Q.front().first, cx = Q.front().second;
        Q.pop();

        int dx[4] = {1, 0, -1, 0};
        int dy[4] = {0, 1, 0, -1};
        rep(i, 4){
            int ex = cx + dx[i], ey = cy + dy[i];
            if(ex < 0 || ex >=C || ey < 0 || ey >= R) continue;
            if (table.at(ey).at(ex) == '#' || dist.at(ey).at(ex) != INF) continue;
            Q.push(make_pair(ey, ex));
            dist.at(ey).at(ex) = dist.at(cy).at(cx) + 1;
        }
        if(table.at(cy).at(cx) >= 'a' && table.at(cy).at(cx) <= 'z'){
            if(alf[table.at(cy).at(cx)-'a']) continue;
            int aaa=table.at(cy).at(cx)-'a';
            assert(aaa <= 25);
            assert(alx[aaa].size() <= 2000 * 2000);
            assert(alx[aaa].size() == aly[aaa].size());
            rep(j, alx[aaa].size()){
                if(alx[aaa][j] == cx && aly[aaa][j] == cy) continue;
                if(dist.at(aly[aaa][j]).at(alx[aaa][j]) != INF) continue;
                Q.push(make_pair(aly[aaa][j], alx[aaa][j]));
                dist.at(aly[aaa][j]).at(alx[aaa][j]) = dist.at(cy).at(cx) + 1;
            }
            alf[table.at(cy).at(cx)-'a'] = true;
        }
    }
    if(dist.at(gy).at(gx) == INF){
        cout << -1 << endl;
        return 0;
    }
    cout << dist.at(gy).at(gx) << endl;
}

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int tesuu[2006][2006];
int used[1000];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
string karifie[2006];
char fie[2006][2006];
int main(){
    for(int i=0;i<2006;i++){
        for(int j=0;j<2006;j++){
            tesuu[i][j]=-1;
        }
    }
    int H,W;

    cin>>H>>W;
    for(int i=0;i<H;i++){
        cin>>karifie[i];
    }
    int gy,gx,sy,sx;
    for(int i=0;i<=H+1;i++){
        for(int j=0;j<=W+1;j++){
            if(i==0||j==0||i==H+1||j==W+1)fie[i][j]='#';
            else fie[i][j]=karifie[i-1][j-1];
            if(fie[i][j]=='S'){sy=i;sx=j;};
            if(fie[i][j]=='G'){gy=i;gx=j;};
        }
    }
    queue<int> Qx,Qy;
    Qy.push(sy);
    Qx.push(sx);
    tesuu[sy][sx]=0;
    while(!Qx.empty()){
        int kariy=Qy.front();Qy.pop();
        int karix=Qx.front();Qx.pop();
        for(int i=0;i<4;i++){
            if(fie[kariy+dy[i]][karix+dx[i]]!='#'){
                if(tesuu[kariy+dy[i]][karix+dx[i]]==-1){
                    Qy.push(kariy+dy[i]);Qx.push(karix+dx[i]);
                    tesuu[kariy+dy[i]][karix+dx[i]]=tesuu[kariy][karix]+1;
                }
            }
        }
        if('a'<=fie[kariy][karix]&&fie[kariy][karix]<='z'){
            if(used[fie[kariy][karix]]==0){
                used[fie[kariy][karix]]=1;
                for(int i=1;i<=H;i++){
                    for(int j=1;j<=W;j++){
                        if(i==kariy&&j==karix)continue;
                        if(fie[kariy][karix]==fie[i][j]){
                            if(tesuu[i][j]==-1){
                                Qy.push(i);Qx.push(j);
                                tesuu[i][j]=tesuu[kariy][karix]+1;
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<tesuu[gy][gx]<<endl;
    return 0;
}
