#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> H(N), W(M);
  for (int i = 0; i < N; i++) cin >> H.at(i);
  for (int i = 0; i < M; i++) cin >> W.at(i);

  sort(H.begin(), H.end());

  vector<long> L, R;
  for (int i = 0; i + 1 < N; i += 2) L.push_back(H.at(i + 1) - H.at(i));
  for (int i = 1; i + 1 < N; i += 2) R.push_back(H.at(i + 1) - H.at(i));

  auto cs = [&](auto V) {
    int n = V.size() + 1;
    vector<long> R(n);
    for (int i = 1; i < n; i++) {
      R.at(i) = R.at(i - 1) + V.at(i - 1);
    }
    return R;
  };

  L = cs(L);
  R = cs(R);

  long ans = LONG_MAX;
  for (auto w : W) {
    int opp = lower_bound(H.begin(), H.end(), w) - H.begin();
    if (opp % 2) opp--;
    long tmp = 0;
    tmp += abs(w - H.at(opp));
    tmp += R.at((N + 1) / 2 - 1) - R.at(opp / 2);
    tmp += L.at(opp / 2);
    ans = min(ans, tmp);
  }
  cout << ans << "\n";
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pr pair<ll, ll>
#define vct vector<ll>
#define vpr vector<pr>

void prn(ll n){ cout << n << "\n";}
void prs(string s){cout << s << "\n";}
void prv(vct v, ll limt = -1){
    if (limt == -1)limt = v.size();
    for (ll i = 0; i < limt; i++)cout << v[i] << " ";
    cout << "\n";
}

void init() {}

ll n, m, k, a, b, c, x, y, z;
ll mod = 1e9 + 7;

vector<string> g(2001);

vpr mp[26];
vct visa(26,0);
bool chk(ll x, ll y, ll m, ll n){
    return ((x<m) && (x>=0) && (y>=0) && (y<n));
}
void solve(){
    cin>>m>>n;
    for(ll i=0;i<m;i++)cin>>g[i];

    pr st,et;
    for(ll i=0;i<m;i++){
        for(ll j=0;j<n;j++){
            char c=g[i][j];
            if(c=='S')st={i,j};
            else if(c=='G')et={i,j};
            else if(isalpha((int)c)){
                ll tp=c-'a';
                mp[tp].push_back({i,j});
            }
        }
    }

    queue<pair<pr, ll> > q;
    q.push({st,0});
    vector<vector<ll> >vis(m, vector<ll>(n,0));
    vis[st.first][st.second]=1;

    while(!q.empty()){
        pair<pr, ll> nd=q.front();q.pop();
        pr cr=nd.first;
        ll vl=nd.second;

        if(cr==et){
            prn(vl);
            return;
        }
        x=cr.first;y=cr.second;
        ll nx=x-1,ny=y;
        if(chk(nx,ny,m,n) && (g[nx][ny]!='#') && (!vis[nx][ny])){
            vis[nx][ny]=1;
            q.push({{nx,ny},vl+1});
        }
        nx=x+1,ny=y;
        if(chk(nx,ny,m,n) && (g[nx][ny]!='#') && (!vis[nx][ny])){
            vis[nx][ny]=1;
            q.push({{nx,ny},vl+1});
        }

        nx=x,ny=y+1;
        if(chk(nx,ny,m,n) && (g[nx][ny]!='#') && (!vis[nx][ny])){
            vis[nx][ny]=1;
            q.push({{nx,ny},vl+1});
        }

        nx=x,ny=y-1;
        if(chk(nx,ny,m,n) && (g[nx][ny]!='#') && (!vis[nx][ny])){
            vis[nx][ny]=1;
            q.push({{nx,ny},vl+1});
        }

        if(g[x][y]!='S' && isalpha(g[x][y])){
            ll tp=g[x][y]-'a';
            if(visa[tp])continue;
            visa[tp]=1;
            for(ll i=0;i<mp[tp].size();i++){
                if(mp[tp][i]!=cr){
                    vis[mp[tp][i].first][mp[tp][i].second]=1;
                    q.push({mp[tp][i],vl+1});
                }
            }
        }

    }
    prn(-1);


}

int main(){
    init();
    ll t = 1;
    // cin >> t;
    while (t--)solve();
    return 0;
}