//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int maxn = 3050;
const int maxm = maxn*maxn*4;
int head[maxm],vis[maxm];
char G[maxn][maxn];
int dis[maxm];
struct Edge{
	int f,t,w,next;
	Edge(int f = 0,int t = 0,int w = 0,int next = 0):f(f),t(t),w(w),next(next){}
}edge[maxm];
int cnt;
const int INF = 1e9;
void addedge(int f,int t,int w){
	edge[cnt] = Edge(f,t,w,head[f]);
	head[f] = cnt++;
}
void dijkstra(int s){
	typedef pair <int,int> node;
	priority_queue<node,vector<node>,greater<node> >q;
	for(int i =1;i< maxm;i++)dis[i] = INF;
	dis[s] = 0;
	q.push(make_pair(dis[s],s));
	while(!q.empty()){
		node tmp = q.top();
		q.pop();
		int u = tmp.second;
		if(vis[u])continue;
		vis[u] = 1;
		for(int i = head[u];~i;i = edge[i].next){
			int v = edge[i].t;
			if(dis[v] > dis[u] + edge[i].w){
				dis[v] = dis[u] + edge[i].w;
				if(!vis[v]){
					q.push(make_pair(dis[v],v));
				}
			}
		}
	}
	return;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	memset(head,-1,sizeof(head));
	int n,m;
	const int B = 2050*2050+1;
	int S = B+100;
	int T = B+101;
	cin >> n >> m;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		for(int j = 1;j <= m;j++){
			G[i][j] = s[j-1];
			if(s[j-1] == 'S')S = i*2010+j;
			if(s[j-1] == 'G')T = i*2010+j;
		}
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			if(G[i][j] != '#'){
				for(int k = -1;k <= 1;k++){
					for(int l = -1;l <= 1;l++)if(abs(k+l) == 1){
						int x = i+k,y = j+l;
						if(x < 1 or x > n or y < 1 or y > m)continue;
						if(G[i][j] != '#')addedge(i*2010+j,x*2010+y,2);
					}
				}
			}
			if(G[i][j] >= 'a' and G[i][j] <= 'z'){
				addedge(i*2010+j,B+G[i][j],1);
				addedge(B+G[i][j],i*2010+j,1);
			}
		}
	}
	dijkstra(S);
	if(dis[T] == INF)cout << -1;
	else cout << dis[T]/2;
	return 0;
}

/**
  *  Author: dhruv_gheewala
  *  Problem: __________
  *  Date:   13.12.2020
  *  Time:   17:51:47
**/

#if __has_include("debug.h")
    #include "debug.h"
#else
    #include<bits/stdc++.h>
    using namespace std;
    
    #define fundri 108
    #define debug(...) 1729
#endif

#define endl '\n'
#define int int64_t

typedef pair<int, int> pii;
typedef vector<int> vi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}

bool in_range(int x, int l, int r) {return l <= x && x <= r;}
template<typename H, typename ...T>void inp(H &head) {cin >> head;}
template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}
template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}
template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}

// Multi-Dimension Vector
// Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())
template<int D, typename T> struct vec : public vector<vec<D - 1, T>>
{
    static_assert(D >= 1, "Vector dimensions must be greater than zero !!");
    template<typename... Args>
    vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}
};

template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};

const int inf = 1e15;
const bool testcases = false;

void init_main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef DHRUV_GHEEWALA
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif
}

void solve(int tc)
{
    int n, m;
    cin >> n >> m;

    vi a(m);
    cin >> a;

    sort(a.begin(), a.end());
    
    reverse(a.begin(), a.end());
    a.emplace_back(0);

    reverse(a.begin(), a.end());
    a.emplace_back(n + 1);
    m++;

    int mini = inf;
    for(int i = 1; i <= m; i++) {
        if(a[i] - a[i - 1] > 1)
            mini = min(mini, a[i] - a[i - 1] - 1);
    }

    debug(mini, a);
    int res = 0;
    for(int i = 1; i <= m; i++)
        res += ceil(1.00 * (a[i] - a[i - 1] - 1) / mini);
    cout << res << endl;
}

int32_t main(int32_t argc, char **argv)
{
    init_main();

    int TC = 1;
    if(testcases)
        cin >> TC;

    for(int tc = 1; tc <= TC; ++tc) {
        solve(tc);
        fundri;
    }
    return 0;
}