#include <iostream>
#include <string>
#include <vector>
#include <map>
using ll=long long;
using namespace std;

int main(){
    int N;
    ll X;
    cin>>N>>X;

    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    vector<ll> C(N+1);
    C[0]=1000000000000000000;
    for(int i=1;i<=N;i++){
        C[i]=A[N-i];
    }

    vector<map<ll,ll>> dp(N+1);
    dp[0][X]=1;
    for(int i=1;i<N+1;i++){
        for(auto p:dp[i-1]){
            ll a=p.first%C[i];
            dp[i][a]+=p.second;
            ll b=C[i]-a;
            ll num=p.first/C[i]+1;
            if(C[i]*num<C[i-1] && a!=0){
                dp[i][b]+=p.second;
            }
        }
    }
    /*
    for(int i=0;i<N+1;i++){
        for(auto p:dp[i]){
            cout<<"{"<<p.first<<","<<p.second<<"} ";
        }
        cout<<endl;
    }
    //*/
    cout<<dp[N][0]<<endl;

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define endl "\n"
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// const ll mod = 1e9 + 7;
const ll mod = 998244353;
const ll sz = 1e3 + 5;
vector<ll> fac(sz, 1);
vector<bool> prime(sz, true);
vector<ll> fib(sz, 0);

ll powmod(ll a, ll b) { ll res=1; a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1) res=res*a%mod; a=a*a%mod; } return res; }
ll nCr(ll n, ll r) { return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r], mod-2) ) % mod ); }
ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a,b)) * b; }

// * Always PASS arrays, strings etc to functions BY REFERENCE

void solve() {
    int n, m;
    cin >> n >> m;
    
    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>> > pq;
    vector<string> arr(n);
    vector<vector<int>> dist(n, vector<int>(m, 1e9));
    vector<vector<pair<int,int> > > pos(26);
    for (int j = 0; j < n; j++) {
        cin >> arr[j];
        for (int i = 0; i < m; i++) {
            if (arr[j][i] == 'S') {
                pq.push({0, j, i});
                dist[j][i] = 0;
            }
            else if (arr[j][i] >= 'a' && arr[j][i] <= 'z')
                pos[arr[j][i]-'a'].push_back({j,i});
        }
    }
    
    while (!pq.empty()) {
        auto fr = pq.top();
        pq.pop();
        int len = fr[0], i = fr[1], j = fr[2];
        
        if (arr[i][j] == 'G') {
            cout << dist[i][j] << endl;
            return;
        }
        
        vector<int> dx = {-1, 1, 0, 0};
        vector<int> dy = {0, 0, 1, -1};
        
        for (int k = 0; k < 4; k++) {
            int ii = i + dx[k], jj = j + dy[k];
            if (ii < 0 || ii >= n || jj < 0 || jj >= m || arr[ii][jj] == '#')
                continue;
            if (dist[ii][jj] <= len + 1)
                continue;
            
            dist[ii][jj] = len + 1;
            pq.push({dist[ii][jj], ii, jj});
        }
        
        if (!(arr[i][j] >= 'a' && arr[i][j] <= 'z'))
            continue;
        
        for (auto p: pos[arr[i][j]-'a']) {
            int ii = p.fi, jj = p.se;
            if (dist[ii][jj] <= len + 1)
                continue;
            dist[ii][jj] = len + 1;
            pq.push({dist[ii][jj], ii, jj});
        }
      	pos[arr[i][j]-'a'].clear();
    }
    
    cout << -1 << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
//     fib[1] = 1;
//     for (int i = 2; i < sz; i++)
//         fib[i] = (fib[i-1] + fib[i-2]) % mod;
    
//     for (ll i = 2; i < sz; i++)
//         fac[i] = (fac[i-1] * i) % mod;
    
//     prime[0] = prime[1] = false;
//     for (ll i = 2; i*i < sz; i++) {
//         if (!prime[i])
//             continue;
        
//         for (ll j = i*i; j < sz; j += i)
//             prime[j] = false;
//     }
    
    int t = 1;
    // cin >> t;
    
    while(t--) {
        solve();
    }
    
	return 0;
}