#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define repl(i, a, b) for(int i = a; i < int(b); i++)
#define rep(i, n) repl(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    int N;
    ll X;
    cin >> N >> X;
    vector<ll> a(N);
    rep(i, N) cin >> a[i];

    vector<map<ll, ll>> dp(60);
    dp[0][X] = 1;

    rep(i, N) {
        for(const auto &[j, val] : dp[i]) {
            if(i < N - 1) {
                ll r = j % a[i + 1];
                dp[i + 1][j - r] += val;
                if(r > 0) dp[i + 1][j + a[i + 1] - r] += val;
            } else {
                dp[i + 1][0] += val;
            }
        }
    }

    cout << dp[N][0] << "\n";
}
#include <bits/stdc++.h>
using namespace std;

const int N = 2005;

int n, m;
char s[N][N];

pair<int, int> st, en;
vector<pair<int, int>> teleports[26];

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

int dist[N][N];

bool used[26];
bool vis[N][N];
void bfs() {
  queue<pair<int, int>> q;
  q.push(st);
  vis[st.first][st.second] = true;
  dist[st.first][st.second] = 0;

  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();

    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k], ny = y + dy[k];

      if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
      if (s[nx][ny] == '#') continue;
      if (vis[nx][ny]) continue;
      vis[nx][ny] = true;
      dist[nx][ny] = dist[x][y] + 1;
      q.emplace(nx, ny);
    }

    if ('a' <= s[x][y] && s[x][y] <= 'z' && !used[s[x][y] - 'a']) {
      used[s[x][y] - 'a'] = true;

      for (pair<int, int> p : teleports[s[x][y] - 'a']) {
        int nx, ny;
        tie(nx, ny) = p;

        if (vis[nx][ny]) continue;
        vis[nx][ny] = true;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }
}

int solve() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s[i]);
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        st = make_pair(i, j);
      } else if (s[i][j] == 'G') {
        en = make_pair(i, j);
      } else if ('a' <= s[i][j] && s[i][j] <= 'z') {
        teleports[s[i][j] - 'a'].emplace_back(i, j);
      }
    }
  }

  bfs();

  if (!vis[en.first][en.second]) printf("-1\n");
  else printf("%d\n", dist[en.first][en.second]);
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  for (int tc = 0; tc < t; ++tc) {
    solve();
  }
  return 0;
}
