#include <iostream>
#include <vector>
//#include <string>
#include <algorithm>
//#include <math.h>
#include <queue>
//#include <stack>
//#include <iomanip>

// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>

typedef long long LL;
//typedef long double LD;
using namespace std;
 
//#define MOD 1000000007
//#define MOD 998244353
//#define MAX 100100
#define NIL -1
//#define INF 1000000000000000000
//const LL SEG_VAL = 1 << 19;

vector<vector<pair<LL, LL>>> tele(26);
vector<bool> tele_visited(26, false);
vector<vector<LL>> a_num(2010, vector<LL>(2010, 0));
vector<vector<LL>> dist(2010, vector<LL>(2010, NIL));
vector<vector<bool>> visited(2010, vector<bool>(2010, false));
LL h;
LL w;

vector<LL> dx={0,1,0,-1};
vector<LL> dy={1,0,-1,0};

void bfs(LL y, LL x){
    queue<pair<LL, LL>> q;
    q.push(make_pair(y, x));
    visited[y][x]=true;
    pair<LL, LL> u;
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(LL i=0; i<4; i++){
            LL y_new=u.first+dy[i];
            LL x_new=u.second+dx[i];
            if(y_new<0 || y_new>=h){
                continue;
            }
            if(x_new<0 || x_new>=w){
                continue;
            }
            if(a_num[y_new][x_new]==NIL){
                continue;
            }
            if(visited[y_new][x_new]==true){
                continue;
            }
            dist[y_new][x_new]=dist[u.first][u.second]+1;
            visited[y_new][x_new]=true;
            q.push(make_pair(y_new, x_new));
        }
        LL tmp=a_num[u.first][u.second];
        if(tmp!=0){
            if(tele_visited[tmp-1]!=true){
                for(LL i=0; i<tele[tmp-1].size(); i++){
                    LL y_new=tele[tmp-1][i].first;
                    LL x_new=tele[tmp-1][i].second;
                    if(visited[y_new][x_new]==true){
                        continue;
                    }
                    dist[y_new][x_new]=dist[u.first][u.second]+1;
                    visited[y_new][x_new]=true;
                    q.push(make_pair(y_new, x_new));                
                }
                tele_visited[tmp-1]=true;
            }
        }
    }
}

int main(){
    cin >> h >> w;
    char a;
    LL h_s;
    LL w_s;
    LL h_e;
    LL w_e;
    for(LL i=0; i<h; i++){
        for(LL j=0; j<w; j++){
            cin >> a;
            if(a=='#'){
                a_num[i][j]=NIL;
            }else if(a=='S'){
                h_s=i;
                w_s=j;
            }else if(a=='G'){
                h_e=i;
                w_e=j;
            }else if(a=='.'){
            }else{
                LL tmp=a-'a';
                tele[tmp].push_back(make_pair(i,j));
                a_num[i][j]=tmp+1;
            }
        }
    }

    dist[h_s][w_s]=0;
    bfs(h_s, w_s);

    /*
    for(LL i=0; i<h; i++){
        for(LL j=0; j<w; j++){
            //cout << a_num[i][j];
            cout << dist[i][j];
        }
        cout << endl;
    }
    */

    /*
    for(LL i=0; i<3; i++){
        for(LL j=0; j<tele[i].size(); j++){
            cout << tele[i][j].first << " " << tele[i][j].second << endl;
        }
    }
    */

    cout << dist[h_e][w_e] << endl;

    return 0;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
const int MAX = 1000;
class shuturyoku_unko {
public:
	char C[MAX * 4];
	constexpr shuturyoku_unko() : C() {
		rep(i, MAX) {
			int X = i;
			rep(j, 3) {
				C[i * 4 + 2 - j] = '0' + X % 10;
				X /= 10;
			}
			C[i * 4 + 3] = ' ';
		}
	}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
	if (owad < di) {
		fwrite(dn, 1, di - dn, stdout);
		di = dn;
	}
	if (A >= 1000) {
		int dig = 1;
		if (A >= 100000) dig = 3;
		else if (A >= 10000) dig = 2;
		memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig);
		memcpy(di + dig, f.C + A % 1000 * 4, 4);
		di += dig + 4;
	}
	else {
		int dig = 1;
		if (A >= 100) dig = 3;
		else if (A >= 10) dig = 2;
		memcpy(di, f.C + A * 4 + 3 - dig, dig + 1);
		di += dig + 1;
	}
}

int N;
int to[400001], ne[400001], he[200001];
int Q[200001], vis[200001], p, q;
ll are[200001];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	N = getint();
	rep1(i, N * 2 - 2) {
		int a = getint(), b = getint();
		to[i] = b;
		ne[i] = he[a];
		he[a] = i++;
		to[i] = a;
		ne[i] = he[b];
		he[b] = i;
	}

	Q[q++] = 1;
	while (p < q) {
		int u = Q[p++];
		vis[u] = -1;
		for (int i = he[u]; i; i = ne[i]) {
			if (vis[to[i]] == 0) {
				Q[q++] = to[i];
			}
		}
	}
	int tmp = Q[N - 1];
	p = 0;
	q = 0;
	Q[q++] = tmp;
	vis[tmp] = 0;
	while (p < q) {
		int u = Q[p++];
		for (int i = he[u]; i; i = ne[i]) {
			if (vis[to[i]] < 0) {
				vis[to[i]] = u;
				Q[q++] = to[i];
			}
		}
	}
	ll m = (1ll << 20) - 1, m0 = ~m, m1 = 1ll << 20;
	for (int i = N - 1; i >= 0; i--) {
		int u = Q[i];
		ll kari = (are[u] & m0) + m1 + u;
		if (are[vis[u]] < kari) are[vis[u]] = kari;
	}

	int k = 0;
	q = 0;
	Q[q++] = tmp;
	while (q) {
		k++;
		int u = Q[q - 1];
		if (u < 0) q--;
		else {
			vis[u] = -k;
			Q[q - 1] = -u;
			int nanika = are[u] & m;
			if (nanika) Q[q++] = nanika;
			for (int i = he[u]; i; i = ne[i]) {
				int v = to[i];
				if (vis[v] >= 0 && v != nanika) {
					Q[q++] = v;
				}
			}
		}
	}

	rep1(i, N) putint(-vis[i]);
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}