#include <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const int INF = 1e9;

VVI dx, dy;
int dp[510][510][2];
int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  dx.push_back({1,2,2,1,-1,-2,-2,-1});
  dx.push_back({1,1,-1,-1});
  dy.push_back({-2,-1,1,2,2,1,-1,-2});
  dy.push_back({-1,1,1,-1});

  int H, W; cin >> H >> W;
  VS vs(H);
  REP(i,H) cin >> vs[i];
  fill((int*)dp, (int*)dp+510*510*2, INF);

  queue<tuple<int,int,int>> q;
  int gx, gy;
  REP(y,H) REP(x,W)
	if(vs[y][x] == 'S'){
	  dp[y][x][0] = 0;
	  q.push(make_tuple(x,y,0));
	}
	else if(vs[y][x] == 'G')
	  gx = x, gy = y;

  while(!q.empty()){
	int x, y, t;
	tie(x,y,t) = q.front(); q.pop();
	REP(i,SZ(dx[t])){
	  int tx = x + dx[t][i];
	  int ty = y + dy[t][i];
	  if(0 <= tx && tx < W && 0 <= ty && ty < H){
		int nt = (vs[ty][tx] == 'R'? 1-t: t);
		if(dp[ty][tx][nt] == INF){
		  dp[ty][tx][nt] = dp[y][x][t] + 1;
		  q.push(make_tuple(tx,ty,nt));
		}
	  }
	}
  }
  /*
  REP(y,H){
	REP(x,W) cout << dp[y][x][0] << " ";
	cout << endl;
  }
  cout<<endl;
  REP(y,H){
	REP(x,W) cout << dp[y][x][1] << " ";
	cout << endl;
  }
  */
  int tmp = min(dp[gy][gx][0], dp[gy][gx][1]);
  cout << (tmp == INF? -1: tmp) << endl;
    
  return 0;
}