#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cerr<<"fk"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
const int N=2100;
int h,w,vis[N][N],dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
char mat[N][N];
vector< pair<int,int> > idx[30];
bool ok(int x,int y){
    if(x<1 || x>h || y<1 || y>w) return false;
    if(mat[x][y]=='#') return false;
    if(vis[x][y]==1) return false;
    return true;
}
int bfs(int sx,int sy){
    queue< pair< pair<int,int>,int > > q;
    q.push(mp(mp(sx,sy),0));
    vis[sx][sy]=1;
    while(!q.empty()){
        pair< pair<int,int>,int > now=q.front();
        q.pop();
        int nx=now.fi.fi,ny=now.fi.se;
        if(mat[nx][ny]=='G') return now.se;
        char temp=mat[nx][ny];
        if(temp>='a' && temp<='z'){
            for(pair<int,int> np:idx[int(temp-'a')]){
                vis[np.fi][np.se]=1;
                q.push(mp(mp(np.fi,np.se),now.se+1));
            }
            idx[int(temp-'a')].clear();
        }
        for(int i=0; i<4; ++i){
            int tx=nx+dir[i][0];
            int ty=ny+dir[i][1];
            if(ok(tx,ty)){
                //if(mat[tx][ty]=='G') return now.se+1;
                vis[tx][ty]=1;
                q.push(mp(mp(tx,ty),now.se+1));
            }
        }
    }
    return -1;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif
    cin>>h>>w;
    int sx,sy;
    for1(i, h){
        for1(j, w){
            cin>>mat[i][j];
            if(mat[i][j]>='a' && mat[i][j]<='z'){
                int num=int(mat[i][j]-'a');
                idx[num].eb(mp(i,j));
            }
            if(mat[i][j]=='S'){
                sx=i;
                sy=j;
            }
        }
    }
    ms(vis,0);
    cout<<bfs(sx,sy)<<'\n';
#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

#include <bits/stdc++.h>
#define MOD (long long)(1E9+7)
#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;
int table[2000][2000];
void bfs(queue<pair<int,int>> &que, vector<vector<pair<int,int>>> &warp, int &now, int &w, int &h) {
  int n = que.size();
  rep(lll,n) {
	auto x = que.front();
    int i = x.first;
 	int j = x.second;
    que.pop();
    if(table[i][j] >=10000000) {
    	int abc = table[i][j] -10000000-1;
      	table[i][j] = now+1;
      	rep(k, warp[abc].size()) {
        	auto yy = warp[abc][k];
          	if(table[yy.first][yy.second] > now+1) {
              	que.push(make_pair(yy.first,yy.second));
              	table[yy.first][yy.second] = -1;
            }
        }
    }
    if(table[i][j] > now || table[i][j] == -1) {
      table[i][j] = now;
      if(i>0 && (table[i-1][j] == -1 || table[i-1][j]>now+1)) {
      	que.push(make_pair(i-1,j));
      }
      if(i<h-1 && (table[i+1][j]  == -1 || table[i+1][j]>now+1)) {
      	que.push(make_pair(i+1,j));
      }
      if(j>0 && (table[i][j-1] == -1 || table[i][j-1]>now+1)) {
      	que.push(make_pair(i,j-1));
      }
      if(j<w-1 && (table[i][j+1] == -1 || table[i][j+1]>now+1) ) {
      	que.push(make_pair(i,j+1));
      }
      
    }
  }
  if(!que.empty()) {
    now++;
  	bfs(que, warp, now,w,h); 
  }
}
int main(){
  	int h,w,sx,sy,gx,gy;
  	string s;
	cin >> h >> w;
  	vector<vector<pair<int,int>>> warp(26);
  	rep(i,h)rep(j,w) table[i][j] = -1;
  	rep(i,h) {
    	cin >> s; 
      	rep(j,w) {
        	if(s[j] >= 'a' && s[j] <= 'z') {
            	table[i][j] = 10000000+s[j]-'a'+1;
              	warp[(s[j]-'a')].push_back(make_pair(i, j));
            } else if(s[j] == 'S') {
              	sx = j; sy = i;
            } else if(s[j] == 'G') {
               gx =  j; gy = i;
            } else if(s[j] == '.') {
              continue;
            } else if(s[j] == '#') {
             	table[i][j] = -2;
            } 
        }
    }
  	
 	queue<pair<int,int>> que;
  	que.push(make_pair(sy,sx));
  	int now=0;
  	bfs(que, warp, now,w,h);
	cout << table[gy][gx] << endl;
}