//fake_name
#include<bits/stdc++.h>
#define int long long 
using namespace std ; 
#define F first 
#define S second  

int MOD = 998244353 ; 
int mod = 1e9 + 7 ; 
string s[2005] ; 
int vis[2005][2005] ; 
int n , m ; 
int check( int i , int j )
{
	if( i >= n || j >= m || i < 0 || j < 0 ) 
		return 0 ; 
	if( vis[i][j] || s[i][j] == '#' ) 
		return 0 ; 
	return 1 ; 
}
signed main()
{
    ios_base::sync_with_stdio(false) ; 
    cin.tie(0) ; cout.tie(0) ; 
    cin >> n >> m ; 
    for( int i = 0 ; i < n ; i++ ) 
    	cin >> s[i] ; 
    vector<pair<int,int> > adj[30] ;  
    pair<int,int> start , end ; 
    for( int i = 0 ; i < n ; i++ ){
    	for( int j = 0 ; j < m ; j++ ){
    		if( s[i][j] == '.' || s[i][j] == '#' ) 
    			continue ; 
    		if( s[i][j] == 'S' ) 
    			start = { i , j } ; 
    		else if( s[i][j] == 'G' ) 
    			end = { i , j } ; 
    		else{
    			//cout << s[i][j] << endl ; 
    			adj[s[i][j]-'a'].push_back( { i , j } ) ; 
    		}
    	}
    }
    queue<pair<int,int> > q ; 
    q.push(start) ; 
    vis[start.F][start.S] = 1 ; 
    int h[26] = {0} ; 
    while( !q.empty() ){
    	pair<int,int> p = q.front() ; 
    	q.pop() ; 
    	int i = p.F , j = p.S ; 
    	if( check( i+1 , j )){
    		vis[i+1][j] = vis[i][j]+1 ; 
    		q.push( { i+1 , j } ) ; 
    	}
    	if( check( i-1 , j ) ){
    		vis[i-1][j] = vis[i][j] + 1 ; 
    		q.push( { i-1 , j } ) ; 
    	}
    	if( check( i , j+1 ) ){
    		vis[i][j+1] = vis[i][j] + 1 ; 
    		q.push( { i , j+1 } ) ; 
    	}
    	if( check( i , j-1 ) ){
    		vis[i][j-1] = vis[i][j] + 1 ; 
    		q.push( { i , j-1 } ) ; 
    	}
    	if( isalpha( s[i][j] ) && s[i][j] > 'Z' ){
    		//cout << s[i][j] << endl ; 
    		if( h[s[i][j]-'a' ] ) 
    			continue ; 
    		for( int k = 0 ; k < (int)adj[s[i][j] - 'a' ].size() ; k++ ){
    			
    			if( check( adj[s[i][j] - 'a'][k].F , adj[s[i][j]-'a'][k].S) ){
    				vis[adj[s[i][j]-'a'][k].F][adj[s[i][j]-'a'][k].S] = vis[i][j]+1 ;
    				q.push( adj[s[i][j]-'a'][k] ) ;  
    				 
    			}
    		}
    		h[s[i][j]-'a'] = 1 ; 
    	}
    }
    if( vis[end.F][end.S] == 0 ) 
    	cout << -1 ; 
    else 
    	cout << vis[end.F][end.S] - 1 ; 
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
}   
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int64_t i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int64_t i=(a),_b=(b);i>=_b;--i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); }
res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<string> grid(h);
    vector<VI> dp(h, VI(w, -1));
    REP(i, h) cin >> grid[i];
    queue<PII> Q;
    vector<vector<PII>> letters(26);
    REP(i, h) {
    	REP(j, w) {
    		if (grid[i][j] == 'S') {
    			Q.push({i, j});
    			dp[i][j] = 0;
    		}
    		if (grid[i][j] >= 'a' && grid[i][j] <= 'z') {
    			letters[grid[i][j] - 'a'].pb({i, j});
    		}
    	}
    }
    vector<bool> F(26, false);
    int d1[4] = {1, -1, 0, 0};
    int d2[4] = {0, 0, -1, 1};
    while (!Q.empty()) {
    	auto [r, c] = Q.front();
    	Q.pop();
    	if (grid[r][c] == 'G') {
    		cout << dp[r][c] << "\n";
    		return 0;
    	}
    	if (grid[r][c] >= 'a' && grid[r][c] <= 'z' && !F[grid[r][c] - 'a']) {
    		F[grid[r][c] - 'a'] = true;
    		for (auto p : letters[grid[r][c] - 'a']) {
    			if (p.first == r && p.second == c) continue;
    			if (dp[p.first][p.second] != -1) continue;
    			Q.push({p.first, p.second});
    			dp[p.first][p.second] = dp[r][c] + 1;
    		}
    	}
    	FOR(i, 0, 3) {
    		int rr = r + d1[i];
    		int cc = c + d2[i];
    		if (rr < 0 || rr >= h || cc < 0 || cc >= w) continue;
    		if (grid[rr][cc] == '#') continue;
    		if (dp[rr][cc] != -1) continue;
    		dp[rr][cc] = dp[r][c] + 1;
    		Q.push({rr, cc});
    	}
    }
    cout << "-1\n";
}