#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;
}



//BELIEVE IN YOURSELF
//next_permutation

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define maxx 100005
#define sec second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(),v.end());
#define sortr(v) sort(v.rbegin(),v.rend());
#define w() ll _;cin>>_;while(_--)
#define edge pair<ll,ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\n"
#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define lb(v,x) std::lower_bound(v.begin(),v.end(),x)
#define ub(v,x) std::upper_bound(v.begin(),v.end(),x)

using namespace std;

ll dx[]={-1,0,1,0};
ll dy[]={0,-1,0,1};

ll n,m;

bool isvalid(ll x,ll y)
{
	if(x>=0 && y>=0 && x<n && y<m)
	{
		return true;
	}
	return false;
}

int main()
{
		fast;

		cin>>n>>m;
		ll i,j;
		string s;
		vector<string>g;
		
		for(i=0;i<n;i++)
		{
			cin>>s;
			g.pb(s);
		}		
		
		ll a[n+1][m+1];
//		ll vis[n+1][m+1];
		
		
		ll inf=1e17;
		ll sx=0,sy=0,ex=0,ey=0;
		
		vector<vector<pair<ll,ll> > >tp(26);
		
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				a[i][j]=inf;
				if(g[i][j]=='S')
				{
					a[i][j]=0;
					sx=i;
					sy=j;
				}
				if(g[i][j]=='G')
				{
					ex=i;
					ey=j;
				}
				if(g[i][j]>='a' && g[i][j]<='z')
				{
					char c=g[i][j];
					tp[c-'a'].pb(mp(i,j));
				}
			}
		}
		
		
		queue<pair<ll,ll> >q;
		
		q.push(mp(sx,sy));
		
		while(!q.empty())
		{
			ll x,y;
			pair<ll,ll>now =q.front();
			q.pop();
			
			x=now.fi;
			y=now.sec;
			
			for(i=0;i<4;i++)
			{
				ll nx,ny;
				pair<ll,ll>next = mp(x+dx[i],y+dy[i]);
				
				nx=next.fi;
				ny=next.sec;
				
				if(isvalid(nx,ny))
				{
					if(g[nx][ny]!='#' && a[nx][ny]==inf)
					{
						a[nx][ny]=a[x][y]+1;
						q.push(mp(nx,ny));
					}
				}
				
				
			}
			
			char c=g[x][y];
			
			if(islower(c))
			{
				for(auto next:tp[c-'a'])
				{
					ll nx,ny;
					nx=next.fi;
					ny=next.sec;
					
					if(a[nx][ny]==inf)
					{
						a[nx][ny]=a[x][y]+1;
						q.push(mp(nx,ny));
					}
				}
			
				tp[c-'a'].clear();
			
			}
		
			
			
		}
		
		ll ans=a[ex][ey];
		
		if(ans==inf)
		{
			cout<<-1<<Endl;
		}
		else
		{
			cout<<ans<<endl;
		}
		
																										
	
	return 0;
}
