#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define st first
#define nd second
#define endl '\n' //toggle off for interactive problems
#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << e << ' '; cerr << '\n'; //vector, set
#define what_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << x[i] << ' '; cerr <<  '\n'; //n first element of array
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << cerr_pair(e) << ' '; cerr << '\n'; //vector, set
#define pwhat_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << cerr_pair(x[i]) << ' '; cerr <<  '\n'; //n first element of array
// #define int long long
// #define pow mypow
 
typedef pair<int,int> pii;
const int N = 2e3+5;
const int INF = 1e9+5;
const int MOD = 1e9+7;
 
int n, m;
char a[N][N];
int d[N][N];
bool vis[N][N], visc[26];
vector<pii> g[26];
queue<pii> q;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
int gx, gy;
 
int32_t main()
{
	IOS
	// freopen("input.txt", "r", stdin);
	cin >> n >> m;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			cin >> a[i][j];
			if (a[i][j] == 'S') {
				vis[i][j] = true;
				q.push({i, j});
			} else if (a[i][j] == 'G') {
				gx = i;
				gy = j;
			} else if (a[i][j] != '.' && a[i][j] != '#') {
				g[a[i][j]-'a'].push_back({i, j});
			}
		}
	}
	while(!q.empty()){
		auto [x, y] = q.front(); q.pop();
		for (int i=0; i<4; i++) {
			int nx = x + di[i];
			int ny = y + dj[i];
			if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
			if (vis[nx][ny] || a[nx][ny] == '#') continue;
			d[nx][ny] = d[x][y] + 1;
			vis[nx][ny] = true;
			q.push({nx, ny});
		}
		if (a[x][y] == 'G') break;
		if (a[x][y] != '.' && a[x][y] != '#' && !visc[a[x][y]-'a']) {
			visc[a[x][y]-'a'] = true;
			for(auto [nx, ny]:g[a[x][y]-'a']) {
				if (vis[nx][ny]) continue;
				d[nx][ny] = d[x][y] + 1;
				vis[nx][ny] = true;
				q.push({nx, ny});
			}
		}
 
	}
	int ans = d[gx][gy];
	cout << ((ans)?ans:-1) << endl;
	return 0;
}
#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 9999999999
#define mod 1000000007
#define eps 1e-12
using namespace std;
typedef pair<ll,ll>du;
ll n,m,a,b,c,d,dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},ans=-1;
vector<du>edge[200];
queue<du>q;
ll dis[2005][2005],gone[2005][2005],done[205];
char s[2005][2005];
int main(){
    ll i,j,k;
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(ll j=1;j<=n;j++)
        cin>>&s[j][1];
    for(ll i=0;i<=200;i++)
        done[i]=0;
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++){
            gone[i][j]=0;
            if(s[i][j]=='S'){
                q.push({i,j});
                dis[i][j]=0;
            }
            else if(s[i][j]>='a'&&s[i][j]<='z')
                edge[s[i][j]].push_back({i,j});
            if(s[i][j]!='S')
                dis[i][j]=INF;
        }
    while(!q.empty()){
        tie(a,b)=q.front();
        q.pop();
        //cout<<a<<" "<<b<<endl;
        if(s[a][b]=='G'){
            ans=dis[a][b];
            break;
        }
        if(!done[s[a][b]]&&s[a][b]>='a'&&s[a][b]<='z'){
            for(auto k:edge[s[a][b]]){
                tie(c,d)=k;
                if(!gone[c][d])
                    q.push({c,d}),gone[c][d]=1,dis[c][d]=dis[a][b]+1;
            }
            done[s[a][b]]=1;
        }
        for(ll i=0;i<4;i++){
            c=a+dx[i],d=b+dy[i];
            if(c<=0||c>n||d<=0||d>m||s[c][d]=='#')
                continue;
            if(!gone[c][d])
                q.push({c,d}),gone[c][d]=1,dis[c][d]=dis[a][b]+1;
        }
    }
    cout<<ans<<endl;
    return 0;
}