#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const ll INF = 10e16;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> s(H);
    rep(i, H) cin >> s[i];

    vector<vector<P>> G(26);
    vector<bool> reached(26, false);
    rep(i, H) rep(j, W) {
        if (s[i][j] >= 'a' && s[i][j] <= 'z') {
            G[s[i][j] - 'a'].push_back({i, j});
        }
    }

    rep(i, H) rep(j, W) {
        if (s[i][j] != 'S') continue;

        vector<vector<ll>> dist(H, vector<ll>(W, INF));
        queue<P> que;

        que.push({i, j});
        dist[i][j] = 0;

        while (!que.empty()) {
            int h = que.front().first;
            int w = que.front().second;
            que.pop();

            if (s[h][w] >= 'a' && s[h][w] <= 'z' &&
                reached[s[h][w] - 'a'] == false) {
                for (auto p : G[s[h][w] - 'a']) {
                    if (p.first == h && p.second == w) continue;
                    if (dist[p.first][p.second] < INF) continue;
                    dist[p.first][p.second] = dist[h][w] + 1;
                    que.push({p.first, p.second});
                    reached[s[h][w] - 'a'] = true;
                }
            }

            for (int direction = 0; direction < 4; direction++) {
                int nh = h + dx[direction];
                int nw = w + dy[direction];
                if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
                if (dist[nh][nw] < INF) continue;
                if (s[nh][nw] == '#') continue;
                dist[nh][nw] = dist[h][w] + 1;
                que.push({nh, nw});

                if (s[nh][nw] == 'G') {
                    cout << dist[h][w] + 1 << endl;
                    return 0;
                }
            }
        }
        cout << -1 << endl;
    }
}
#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;

#define pb push_back
#define ff first
#define ss second
#define MAX 300005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };

string s[2000];
int dist[2000][2000],h,w;
vector<aii> vec[26];
int bfs(int x, int y, int dx, int dy)
{
    queue<aii> q;
    q.push({x,y});
    memset(dist,-1,sizeof dist);
    dist[x][y]=0;
    int already_pushed[26]= {};
    while(!q.empty())
    {
        auto [cx,cy]=q.front();
        q.pop();

        for(int i=0; i<4; i++)
        {
            int nx=cx+xMove[i],ny=cy+yMove[i];
            if(nx>=0 and nx<h and ny>=0 and ny<w and s[nx][ny]!='#' and dist[nx][ny]==-1)
            {
                dist[nx][ny]=dist[cx][cy]+1;
                q.push({nx,ny});
            }
        }

        if(s[cx][cy]>=97 and s[cx][cy]<=122 and !already_pushed[s[cx][cy]-97])
        {
            already_pushed[s[cx][cy]-97]=1;
            for(auto [nx,ny]: vec[s[cx][cy]-97])
            {
                if(dist[nx][ny]==-1)
                {
                    dist[nx][ny]=dist[cx][cy]+1;
                    q.push({nx,ny});
                }
            }
        }
    }

    return dist[dx][dy];
}

int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0),cout.tie(0);
    ios_base::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);
    int TC=1;
    int n,m,q,k;

    //cin>>TC;
    for(int t1=1; t1<=TC; t1++)
    {
        cin>>h>>w;
        for(int i=0; i<h; i++)
            cin>>s[i];
        int sx,sy,dx,dy;
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                if(s[i][j]=='S')
                    sx=i,sy=j;
                else if(s[i][j]=='G')
                    dx=i,dy=j;
                else if(s[i][j]!='#' and s[i][j]!='.')
                    vec[s[i][j]-97].pb({i,j});
            }
        }

        cout<<bfs(sx,sy,dx,dy)<<endl;
    }

    return 0;
}
