#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define fz(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define ffe(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define fill0(a) memset(a,0,sizeof(a))
#define fill1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
int n;
char s[500005],t[500005];
int p1[500005],n1=0;
int p2[500005],n2=0;
int main(){
	scanf("%d%s%s",&n,s+1,t+1);
	for(int i=1;i<=n;i++) if(s[i]=='1') p1[++n1]=i;
	for(int i=1;i<=n;i++) if(t[i]=='1') p2[++n2]=i;
	if(n1<n2||(n1-n2)%2) return puts("-1"),0;
	ll ans=0;
	set<int> st;
	for(int i=1;i<=n1;i++) st.insert(p1[i]);
	for(int i=n2;i;i--){
		set<int>::iterator it=st.lower_bound(p2[i]);
		if(it==st.end()) return puts("-1"),0;
		ans+=*it-p2[i];st.erase(*it);
	}
	vector<int> v;
	ffe(it,st) v.pb(*it);
	for(int i=0;i<v.size();i+=2) ans+=v[i+1]-v[i];
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
#define ll long long
using namespace std;

const int INF = 1e9+7;

int show_matrix(vector<vector<int>> &dp) {
  loop(0, dp.size(), i) {
    loop(0, dp[i].size(), j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll H, W;
  cin >> H >> W;

  ll gx ,gy;


  vector<vector<char>> MAP(H, vector<char>(W, '.'));
  vector<vector<ll>> DIST(H, vector<ll>(W, INF));
  queue<pair<int, int>> Q;
  vector<pair<int, int>> WARP[26];

  loop(0, H, i) {
    loop(0, W, j) {
      char inp;
      cin >> inp;

      if (inp == 'S') {
        Q.push(make_pair(i, j));
        MAP[i][j] = '.';
        DIST[i][j] = 0;
      } else if (inp == 'G') {
        MAP[i][j] = '.';
        gx = i;
        gy = j;
      } else if (inp == '.') {
        MAP[i][j] = '.';
      } else if (inp == '#') {
        MAP[i][j] = '#';
      } else {
        MAP[i][j] = inp;
        ll pos = inp - 'a';
        WARP[pos].push_back(make_pair(i, j));
      }
    }
  }

  ll dx[] = {0, 1, 0, -1};
  ll dy[] = {1, 0, -1, 0};
  while(!Q.empty()) {
    auto top = Q.front(); Q.pop();
    ll x = top.first;
    ll y = top.second;
    ll c = DIST[x][y];

    loop(0, 4, i) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W) {
        if (MAP[nx][ny] == '#' || DIST[nx][ny] <= c+1) continue;
        DIST[nx][ny] = c+1;
        Q.push(make_pair(nx, ny));
      }
    }

    if (MAP[x][y] != '.') {
      ll pos = MAP[x][y] - 'a';
      for (auto &p : WARP[pos]) {
        ll nx = p.first;
        ll ny = p.second;
        if (DIST[nx][ny] > c+1) {
          DIST[nx][ny] = c+1;
          Q.push(make_pair(nx, ny));
        }
        MAP[nx][ny] = '.';
      }
    }
    MAP[x][y] = '.';
  }
  ll result = DIST[gx][gy];
  if (result == INF) {
    print(-1);
  } else {
    print(result);
  }
}
