#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <math.h>
#include <tuple>

#include <set>
#include <unordered_map>
#include <stack>
#include <map>

#include <cstdlib>
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()
using ll = long long;
template<class T>bool chmaxj(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
void YesNo(bool y) { cout << (y ? "Yes" : "No") << endl; } //true==yes


#define INF 21474836

//////////////////////////////////////////////////////////////

int H, W;

int main() {

	int Sx, Sy;
	int Gx, Gy;

	cin >> H >> W;

	vector<vector<char>> a(H, vector<char>(W, 0));
	vector<vector<int>> max(H, vector<int>(W, INF));
	vector<vector<pair<int, int>>> 
		alpha(30,vector<pair<int,int>>(1,pair<int,int>(-1,-1)));
	bool checked[30] = { false };

	for (int y = 0; y < H; y++) {
		string s;
		cin >> s;
		for (int x = 0; x < W; x++) {
			a[y][x] = s[x];

			if (a[y][x] == 'S') {
				Sx = x;
				Sy = y;
			}

			if (a[y][x] == 'G') {
				Gx = x;
				Gy = y;
			}

			if ('a' <= a[y][x] && a[y][x] <= 'z') {
				if (alpha[a[y][x] - 'a'][0].first == -1) {
					alpha[a[y][x] - 'a'][0].first = x;
					alpha[a[y][x] - 'a'][0].second = y;
				}
				else {
					alpha[a[y][x] - 'a'].push_back(pair<int,int>(x,y));
				}
			}
		}
	}

	max[Sy][Sx] = 0;

	queue<pair<int,int>> que;
	que.push(pair<int, int>(Sy,Sx));

	while (!que.empty())
	{
		pair<int, int> p = que.front();
		que.pop();

		int x = p.second;
		int y = p.first;

		if (0 < x) {
			if (a[y][x - 1] != '#') {
				if (max[y][x - 1] > max[y][x] + 1) {
					max[y][x - 1] = max[y][x] + 1;
					que.push(pair<int, int>(y, x - 1));
				}
			}
		}

		if (x < W - 1) {
			if (a[y][x + 1] != '#') {
				if (max[y][x + 1] > max[y][x] + 1) {
					max[y][x + 1] = max[y][x] + 1;
					que.push(pair<int, int>(y, x + 1));
				}
			}
		}

		if (0 < y) {
			if (a[y - 1][x] != '#') {
				if (max[y - 1][x] > max[y][x] + 1) {
					max[y - 1][x] = max[y][x] + 1;
					que.push(pair<int, int>(y - 1, x));
				}
			}
		}

		if (y < H - 1) {
			if (a[y + 1][x] != '#') {
				if (max[y + 1][x] > max[y][x] + 1) {
					max[y + 1][x] = max[y][x] + 1;
					que.push(pair<int, int>(y + 1, x));
				}
			}
		}

		if ('a' <= a[y][x] && a[y][x] <= 'z') {

			int ss = a[y][x] - 'a';
			if (checked[ss] == false) {

				for (int i = 0; i < alpha[ss].size(); i++) {
					int alx = alpha[ss][i].first;
					int aly = alpha[ss][i].second;
					if (max[aly][alx] > max[y][x] + 1) {
						max[aly][alx] = max[y][x] + 1;
						que.push(pair<int, int>(aly, alx));
					}
				}
			}

			checked[ss] = true;

		}

	}

	if (max[Gy][Gx] == INF) {
		cout << -1;
		return 0 ;
	}
	cout << max[Gy][Gx];
	return 0;
}

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MaxN = 1000005;
char s[MaxN<<1], cmd[MaxN];
char res[MaxN<<1];

int main(){
	int L = MaxN+1, R = L-1;
	scanf("%s",cmd);
	int n = strlen(cmd), op = 1;
	for(int i=0; i<n; ++i)
		if(cmd[i] == 'R') op ^= 1;
		else if(op) s[++ R] = cmd[i];
		else s[-- L] = cmd[i];
	if(!op) rep(i,L,L+R-i)
		swap(s[i],s[L+R-i]);
	int lst = L-1; // last one
	res[L-1] = '#';
	for(int i=L; i<=R; ++i)
		if(lst >= L && s[i] == res[lst]) -- lst;
		else res[++ lst] = s[i];
	res[lst+1] = '?'; rep(i,L,lst)
		if(i < lst && res[i] == res[i+1])
			++ i; // skip the two
		else putchar(res[i]);
	return 0;
}