#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
	int n;
	cin >> n;
	vector<int> p(n);
	rep(i, n) {
		cin >> p[i];
		p[i]--;
	}
	vector<int> s(n - 1), m(n - 1);
	rep(i, n) {
		if (i == p[i]) {
			cout << -1 << endl;
			return 0;
		}
		if (i < p[i]) {
			for (int j = i; j < p[i]; j++) {
				s[j]++;
				if (s[j] > 2) {
					cout << -1 << endl;
					return 0;
				}
				m[j] = max(m[j], j - i + 1);
			}
		}
		else {
			for (int j = i; j > p[i]; j--) {
				s[j - 1]++;
				if (s[j - 1] > 2) {
					cout << -1 << endl;
					return 0;
				}
				m[j - 1] = max(m[j - 1], i - j + 1);
			}
		}
	}
	rep(i, n - 1) {
		if (s[i] != 2) {
			cout << -1 << endl;
			return 0;
		}
	}
	vector<pii> ans(n - 1);
	rep(i, n - 1) ans[i] = pii{m[i], i};
	sort(ans.begin(), ans.end());
	rep(i, n - 1) cout << ans[i].second + 1 << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define SZ(a) int((a).size())
#define _REP(_1,_2,_3,_4,name,...) name
#define _REP4(i,b,e,s) for(decltype(e) _b=(b),_e=(e),i=_b+(0<(s)?0:(s));(0<(s)?i<_e:_e<=i);i+=(s))
#define _REP3(i,b,e) for(decltype(e) _b=(b),_e=(e),i=(_b<_e?_b:_b-1);(_b<_e?i<_e:_e<=i);(_b<_e?i++:i--))
#define _REP2(i,n) for(decltype(n) i=0,_n=(n);i<_n;i++)
#define _REP1(n) for(decltype(n) _i=0,_n=(n);_i<_n;_i++)
#define REP(...) _REP(__VA_ARGS__,_REP4,_REP3,_REP2,_REP1)(__VA_ARGS__)
#define FOR(it,c) for(auto&& it=begin(c);it!=end(c);it++)
#define ROF(it,c) for(auto&& it=rbegin(c);it!=rend(c);it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define INT(n) int n;assert(0<scanf("%d",&n))
#define STR(s) string s;cin>>s
#define VS(v,n) vector<string> v(n);REP(i,n){cin>>v[i];}

int main(){
  INT(h); INT(w);
  VS(a, h);
  vector<int> dr = {1, -1, 0, 0}, dc = {0, 0, 1, -1}, checked(26, 0);
  int rg, cg;
  vector<vector<int>> v(h);
  vector<vector<pair<int, int>>> wp(26);
  queue<pair<int, int>> q;
  REP(i, h){
    REP(j, w){
      v[i].PB(-1);
      if (a[i][j] == 'S'){
        v[i][j] = 0;
        q.push(MP(i, j));
      } else if (a[i][j] == 'G'){
        rg = i; cg = j;
      } else if ('a' <= a[i][j] && a[i][j] <= 'z'){
        wp[a[i][j] - 'a'].EB(i, j);
      } 
    }
  }
  while (!q.empty()){
    auto p = q.front(); q.pop();
    int rp = p.first, cp = p.second, r, c;
    REP(i, 4){
      r = rp + dr[i];
      c = cp + dc[i];
      if (r < 0 || h <= r || c < 0 || w <= c){continue;}
      if (a[r][c] == '#'){continue;}
      if (0 <= v[r][c]){continue;} 
      v[r][c] = v[rp][cp] + 1;
      q.push(MP(r, c));
    }
    if ('a' <= a[rp][cp] && a[rp][cp] <= 'z' and !checked[a[rp][cp] - 'a']) {
      for(auto pw: wp[a[rp][cp] - 'a']){
        r = pw.first; c = pw.second;
        if (rp == r && cp == c){continue;}
        if (0 <= v[r][c]){continue;}
        v[r][c] = v[rp][cp] + 1;
        q.push(MP(r, c));
        checked[a[rp][cp] - 'a'] = 1;
      }
    }
  }
  printf("%d\n", v[rg][cg]);
  return 0;
}
