#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
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;
}

template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
	T ret = 1;
	while (n > 0)
	{
		if (n & 1)
		{
			if (p != -1)
				ret = (ret * x) % p;
			else
				ret *= x;
		}
		if (p != -1)
			x = (x * x) % p;
		else
			x *= x;
		n >>= 1;
	}
	return ret;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;
//using namespace atcoder;

void solve()
{
	int n;
	cin>>n;
	string s,t;
	cin>>s>>t;
	ll ans=0;
	int pos=0;
	rep(i,n){
		chmax(pos,i+1);
		if(s[i]=='1'&&t[i]!='1'){
			for(;pos<n;++pos){
				if(s[pos]=='1'){
					s[i]='0';
					s[pos]='0';
					ans+=pos-i;
					break;
				}
			}
		}
		else if(s[i]!='1'&&t[i]=='1'){
			for(;pos<n;++pos){
				if(s[pos]=='1'){
					s[i]='1';
					s[pos]='0';
					ans+=pos-i;
					break;
				}
			}
		}
	}
	if(s==t){
		cout<<ans;
	}
	else{
		cout<<-1;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, j, k) for(int i = j; i < k; i++)
#define print(x) cout << x
#define newline cout << endl
#define space cout << ' '
#define INF 1000000000007
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

int main(){
    int h,w,x,y;
    cin >> h >>w >>x>>y;
    vector<string> s(h);
    rep(i,h) cin >> s[i];
    int ans=1;
    for(int i=x;i<h;i++){
        if(s[i][y-1]=='#') break;
        ans++;
    }
    for(int i=x-2;i>-1;i--){
        if(s[i][y-1]=='#') break;
        ans++;
    }
    for(int i=y;i<w;i++){
        if(s[x-1][i]=='#') break;
        ans++;
    }
    for(int i=y-2;i>-1;i--){
        if(s[x-1][i]=='#') break;
        ans++;
    }
    print(ans);
    return 0;
}