/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define F(i,a,b) for(i=a;i<b;i++)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e16;
const ll MAXN=200200;
ll i,j,mask;
void solve()
{
   ll x,y;
   cin>>x>>y;
   if(x>=y)
   {
       cout<<x-y;
       return ;
   }
   queue<ll> q;
   q.push(y);
   ll lvl=-1;
   ll ans=y-x;
   set<ll> st;
   st.insert(y);
   while(!q.empty())
   {
       lvl++;
       if(lvl>=ans)break;
       ll s=q.size();
       F(i,0,s)
       {
           ll p=q.front();
           q.pop();
           if(p<=x)
           {
               ans=min(ans,lvl+x-p);
           }
           else
           {
               ans=min(ans,lvl+p-x);
               if(p%2)
               {
                   if(st.count(p+1)==0)
                   {st.insert(p+1);q.push(p+1);}
                   if(st.count(p-1)==0)
                   {st.insert(p-1);q.push(p-1);}
               }
               else
               {
                   if(st.count(p/2)==0)
                   {st.insert(p/2);q.push(p/2);}
               }
           }
       }
   }
   cout<<ans;
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#*/
/*ll t,test;
cin>>test;
F(t,1,test+1)
{
    //cout<<"Case #"<<t<<": ";
    solve();
    nl
}*/
solve();
return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=2005;

int vis[N + 100][N + 100];


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

void solve(){
	int h,w;
	cin >> h >> w;
	string s[h];
	
	int x,y;
	
	vector<pii> v[26];
	
	for(int i = 0; i < h; i++){
		cin >> s[i];
		for(int j = 0; j < w; j++){
			if(s[i][j] == 'S'){
				x = i;
				y = j;
			}
			if(s[i][j] >= 'a' && s[i][j] <= 'z'){
				v[s[i][j] - 'a'].pb({i,j});
			}
		}
	}
	
	set<pair<int, pii>> q;
	q.insert({0,{x,y}});
	
	while(!q.empty()){
		auto tmp = *q.begin();
		q.erase(tmp);
		int X = tmp.sc.fr, Y = tmp.sc.sc;
		if(X == N+5){
			if(vis[X][Y]){
				continue;
			}
			vis[X][Y] = 1;
			// we are on a character
			for(pii z:v[Y]){
				q.insert({tmp.fr+1,z});
			}
			continue;
		}
		if(X < 0 || Y < 0 || X >= h || Y >= w || vis[X][Y] || s[X][Y] == '#'){
			continue;
		}
		vis[X][Y] = 1;
		if(s[X][Y] == 'G'){
			cout << tmp.fr / 2;
			return;
		}
		
		for(int i = 0; i < 4; i++){
			q.insert({tmp.fr + 2, {X+dx[i], Y+dy[i]}});
		}
		if(s[X][Y] >= 'a' && s[X][Y] <= 'z'){
			q.insert({tmp.fr+1,{N+5,s[X][Y] - 'a'}});
		}
	}
	
	cout << -1;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}
