#include<bits/stdc++.h>
clock_t __t=clock();
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 111111
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cout<<(clock()-__t)/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	ll mul(ll x,ll y){ull s=1.0*x/mod*y;ll res=1ull*x*y-s*mod;return (res%mod+mod)%mod;}
}
using namespace my_std;

int n,L;
ll a[sz];
ll b[sz];

map<ll,int>mn,mx;

int main()
{
	file();
#define GG return puts("-1"),0
	read(n,L);
	rep(i,1,n) read(a[i]);
	rep(i,1,n) read(b[i]);
	a[0]=b[0]=0,a[n+1]=b[n+1]=L+1;
	rep(i,0,n+1) if (!mn.count(a[i]-i)) mn[a[i]-i]=mx[a[i]-i]=i; else mx[a[i]-i]=i;
	ll ans=0;
	for (int l=0,r;l<=n+1;l=r+1)
	{
		if (b[l]<=a[l]) { r=l; continue; }
		if (!mn.count(b[l]-l)) GG;
		for (r=l;r<=n&&b[r+1]==b[r]+1;++r);
		ans+=mn[b[l]-l]-l;
	}
	for (int r=n+1,l;r>0;r=l-1)
	{
		if (b[r]>=a[r]) { l=r; continue; }
		if (!mx.count(b[r]-r)) GG;
		for (l=r;l&&b[l-1]==b[l]-1;--l);
		ans+=r-mx[b[r]-r];
	}
	cout<<ans;
	return 0;
}
#include<iostream>
#include<list>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
  cout << "(" << p.first << "," << p.second << ")";
  return os;
}
pair<int,int> tpt(vector<vector<pair<int,int>>>& tp,pair<int,int> p,
                  vector<string>& mp){
  char c=mp[p.first][p.second];
  if(not ('a'<= c and c<= 'z')) return {-1,-1};
  int a=c-'a';
  if(tp[a][0]==p) return tp[a][1];
  return tp[a][0];
}
int q(vector<vector<pair<int,int>>>& tp,pair<int,int> p,
                  vector<string>& mp){
  char c=mp[p.first][p.second];
  if(not ('a'<= c and c<= 'z')) return 0;
  if(tp[c-'a'].size()<=1) return 0;
  return 1;
}

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

int main(){
  int h,w; cin >>h >>w;
  vector<string> mp(h); rep(i,h) cin >>mp[i];
  vector<vector<pair<int,int>>> tp(26);
  list<pair<int,int>> que;
  vector<vector<int>> te(h,vector<int>(w,-1));
  rep(i,h){
    rep(j,w){
      if('a'<= mp[i][j] and mp[i][j]<='z'){
        tp[mp[i][j]-'a'].push_back({i,j});
      }
      if(mp[i][j]=='S'){
        que.push_back({i,j});
        te[i][j]=0;
      }
    }
  }
  
  auto illegal
    =[&](pair<int,int> p){
       int f=p.first,s=p.second;
       if(f<0 or s<0 or f>=h or s>=w) return 1;
       if(mp[f][s]=='#') return 1;
       if(te[f][s]>=0) return 1;
       return 0;
     };
  
  
  while(que.size()){
    auto p=*que.begin();
    que.pop_front();
    int f=p.first,s=p.second;
    int cnt=te[f][s];
    if(mp[f][s]=='G'){
      print(te[f][s]);
      return 0;
    }
    auto t=tpt(tp,p,mp);
    if(q(tp,p,mp)){
      int a=mp[f][s]-'a';
      for(auto q:tp[a]){
        if(p==q) continue;
        if(te[q.first][q.second]>=0) continue;
        if(df)print(q);
        que.push_back(q);
        te[q.first][q.second]=cnt+1;
      }
      tp[a].clear();
    }
    rep(i,4){
      pair<int,int> t={f+dx[i],s+dy[i]};
      if(illegal(t)) continue;
      if(df)print(t);
      que.push_back(t);
      te[t.first][t.second]=cnt+1;
    }
    if(df)print("que",que);
  }
  print(-1);
}
