#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fi first
#define se second
#define pb push_back
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

using namespace std;

signed main(){
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    int h,w; cin >> h >> w;
    vector<string> a(h);
    int s,g;
    vector<vector<Pii>> G(h*w+30);
    vector<int> al[30];
    rep(i,h){
        cin >> a[i];
        rep(j,w){
            if(a[i][j]=='S') s=i*w+j;
            else if(a[i][j]=='G') g=i*w+j;
            else if(a[i][j]!='.'&&a[i][j]!='#'){
                int x=(a[i][j]-'a');
                al[x].pb(i*w+j);
            }
        }
    }
    //cout << s <<  " " << g  << endl;
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    rep(i,h){
        rep(j,w){
            rep(k,4){
                if(a[i][j]=='#') continue;
                int nx=i+dx[k],ny=j+dy[k];
                if(0<=nx&&nx<h && 0<=ny&&ny<w && a[nx][ny]!='#'){
                    G[i*w+j].pb({nx*w+ny,1});
                    G[nx*w+ny].pb({i*w+j,1});
                }
            }
        }
    }
    rep(i,26){
        rep(j,al[i].size()){
            G[al[i][j]].pb({h*w+i,1});
            G[h*w+i].pb({al[i][j],0});
        }
    }
    vector<int> d(h*w+30,1e9);
    priority_queue<Pii,vector<Pii>,greater<Pii>> pq; //{dist,v}
    d[s]=0;
    pq.push({0,s});
    while(!pq.empty()){
        Pii tp=pq.top(); pq.pop();
        int v=(int)tp.second;
        if(d[v]<tp.first) continue;
        for(auto u:G[v]){
            if(d[v]+u.se<d[u.fi]){
                d[u.fi]=d[v]+u.se;
                pq.push({d[u.fi],u.fi});
            }
        }
    }
    if(d[g]==1e9) cout << -1 << endl;
    else cout << d[g] << endl;
    return 0;
}
// g++ main.cpp -o main.out && ./main.out
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

template<class T>
vector<pair<T, int>> factorize(T n) {
    vector<pair<T, int>> res;
    for (long long i = 2; (i * i) <= n; i++) {
        if (n % i) continue;
        res.emplace_back(i, 0);
        while((n % i) == 0) {
            n /= i;
            res.back().second++;
        }
    }
    if (n != 1) res.emplace_back(n, 1);
    return res;
}

ll n, ans = LONG_LONG_MAX;
vector<ll> x;
vector<vector<pair<ll, int>>> xf;
map<ll, ll> cnt;

void dfs(ll idx) {
    if (idx == n) {
        ll tmp = 1;
        for(auto &v : cnt) {
            tmp *= v.first;
        }
        chmin(ans, tmp);
        return;
    }
    bool f = false;
    for(auto &v : xf[idx]) {
        if (cnt.count(v.first)) {
            f = true;
            break;
        }
    }
    if (f) {
        dfs(idx + 1);
    }
    else {
        for(auto &v : xf[idx]) {
            if (v.first == 1) continue;
            cnt[v.first]++;
            dfs(idx + 1);
            cnt[v.first]--;
            if (cnt[v.first] == 0) cnt.erase(v.first);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    cin >> n;
    x.resize(n);
    rep(i, n) cin >> x[i];
    xf.resize(n);
    rep(i, n) xf[i] = factorize(x[i]);
    dfs(0);
    cout << ans << endl;
    return 0;
}
