#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll N, X, A[200];
ll dp[200][200][200];

signed main(){
    cin >> N >> X;
    REP(i, N) cin >> A[i];
    ll ans = LONG_LONG_MAX;
    FOR(k, 1, N + 1){
        REP(i, N + 1) REP(j, N + 1) REP(m, k) dp[i][j][m] = LONG_LONG_MAX;
        dp[0][0][X % k] = X;
        REP(i, N){
            REP(j, k + 1){
                REP(m, k){
                    if(dp[i][j][m] == LONG_LONG_MAX) continue;
                    dp[i + 1][j][m] = min(dp[i + 1][j][m], dp[i][j][m]);
                    ll d = (k + (m - A[i]) % k) % k;
                    if(j < k && dp[i][j][m] - A[i] >= 0) dp[i + 1][j + 1][d] = min(dp[i + 1][j + 1][d], dp[i][j][m] - A[i]);
                }
            }
        }
        if(dp[N][k][0] != LONG_LONG_MAX) ans = min(ans, dp[N][k][0] / k);
    }
    PRINT(ans);
    return 0;
}
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define forn(i,x,n) for(int i = x; i < n; ++i)

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
	int n,m,s,g;
	
	cin>>n>>m;
	vector<int> dist(n*m);
	vector<bool> done(n*m,false);
	vector<vector<int>> adj(n*m);
	unordered_map<char,vector<int>> teleport;
	vector<string> a(n);
	forn(i,0,n)
		cin>>a[i];
	forn(i,0,n)
		forn(j,0,m){
			dist[i*m+j]=INT_MAX;
			if(a[i][j]!='#' && a[i][j]!='.')
				teleport[a[i][j]].push_back(i*m+j);
			if(a[i][j]=='S')
				s=i*m+j;
			if(a[i][j]=='G')
				g=i*m+j;
			if(i && a[i-1][j]!='#')
				adj[i*m+j].push_back((i-1)*m+j);
			if(j && a[i][j-1]!='#')
				adj[i*m+j].push_back(i*m+j-1);
			if(i<n-1 && a[i+1][j]!='#')
				adj[i*m+j].push_back((i+1)*m+j);
			if(j<m-1 && a[i][j+1]!='#')
				adj[i*m+j].push_back(i*m+j+1);
		}

	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    dist[s]=0;
    q.push({dist[s],s});

    while(q.size()){
        int f = q.top().second;
        q.pop();
        // if(done[f])
        // 	continue;
        done[f]=true;
        for(auto v: adj[f])
            if(dist[v]>dist[f]+1)
                dist[v]=dist[f]+1, q.push({dist[v],v});
        if(teleport.find(a[f/m][f%m])!=teleport.end()){
        	for(auto v:teleport[a[f/m][f%m]])
	        	if(dist[v]>dist[f]+1)
	                dist[v]=dist[f]+1, q.push({dist[v],v});
        	teleport.erase(a[f/m][f%m]);
        }
    }
	cout<<(dist[g]==INT_MAX?-1:dist[g]);
}