#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1e9+7;
const double pi = acos(-1);
const ll INF = 1LL<<60;

veci dx = {-1,0,1,0};
veci dy = {0,1,0,-1};
int h,w;

void bfs(P s, vector<string> &G, vecvecl &dist, vector<vector<P>> &tel) {
    queue<P> q;
    q.push(s);
    dist[s.first][s.second] = 0;
    veci seen(26);

    while(!q.empty()) {
        P p = q.front();
        q.pop();
        int y = p.first;
        int x = p.second;

        
        if('a' <= G[y][x] && G[y][x] <= 'z') {
            if(!seen[G[y][x]-'a']) {
                for(P to : tel[G[y][x]-'a']) {
                    int ny = to.first;
                    int nx = to.second;
                    if(y == ny && x == nx) continue;
                    if(dist[ny][nx] != -1) continue;
                    dist[ny][nx] = dist[y][x]+1;
                    q.push({ny,nx});
                }
                seen[G[y][x]-'a'] = 1;
            }
        }
        

        for(int dir = 0; dir < 4; dir++) {
            int ny = p.first+dy[dir];
            int nx = p.second+dx[dir];

            if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if(G[ny][nx] == '#') continue;
            if(dist[ny][nx] != -1) continue;
            dist[ny][nx] = dist[y][x] + 1;
            q.push({ny,nx});
        }
    }
}

int main() {
    cin >> h >> w;
    vector<string> G(h);
    REP(i,h) cin >> G[i];
    vector<vector<P>> tel(26);

    int sx,sy,gx,gy;
    REP(i,h) REP(j,w) if(G[i][j] == 'S') sx = j, sy = i;
    else if(G[i][j] == 'G') gy = i, gx = j;
    else if('a' <= G[i][j] && G[i][j] <= 'z') tel[G[i][j]-'a'].push_back({i,j});
    vecvecl dist(h,vecl(w,-1));

    bfs({sy,sx}, G , dist, tel);

    cout << dist[gy][gx] << endl;
}   

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<climits>
#include<utility>
#include<vector>
#include<queue>
#define int long long
using namespace std;
const int MaxN = 500005;
int n, ans;
char s[MaxN], t[MaxN];
queue<int> pos;

inline int Read(){
	int num = 0, op = 1;
	char ch = getchar();
	while(!isdigit(ch)){
		if(ch == '-') op = -1;
		ch = getchar();
	}
	while(isdigit(ch)){
		num = num * 10 + ch - '0';
		ch = getchar();
	}
	return num * op;
}

signed main(){
	n = Read();
	scanf("%s%s", s+1, t+1);
	for(int i=1; i<=n; i++)
		if(s[i] == '1') pos.push(i);
	for(int i=1; i<=n; i++){
		if(s[i] == t[i]){
			if(s[i] == '1') pos.pop();
			continue;
		}
		else if(s[i] == '1' && t[i] == '0'){
			if(pos.empty()){
				printf("-1");
				return 0;
			}
			int p1 = pos.front();
			pos.pop();
			if(pos.empty()){
				printf("-1");
				return 0;
			}
			int p2 = pos.front();
			pos.pop();
			ans += (p2 - p1);
			s[p1] = s[p2] = '0';
		}
		else if(s[i] == '0' && t[i] == '1'){
			if(pos.empty()){
				printf("-1");
				return 0;
			}
			int p = pos.front();
			pos.pop();
			ans += (p - i);
			s[p] = '0', s[i] = '1';
		}
	}
	printf("%lld", ans);
	return 0;
}