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

const double pi = 3.141592653589793;
int inf = 1001001001;
ll INF = 1001001001001001001;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
ll MOD = 1000000007;
using P = pair<ll, ll>;


double eps = 1e-12;


int main() {
    int N;cin >> N;
    string S;cin >> S;
    string T;cin >> T;
    vector<int> cnts;
    vector<int> cntt;
    vector<int> places;
    vector<int> placet;
    ll cnt = 0;
    rep(i, N) {
        if (S[i] == '0') {
            cnt++;
            cnts.pb(i);
            places.pb(cnt);
        }
    }
    cnt = 0;
    rep(i, N) {
        if (T[i] == '0') {
            cnt++;
            cntt.pb(i);
            placet.pb(cnt);
        }
    }
    if (cnts.size() !=cntt.size()) {
        cout << -1 << endl;
        return 0;
    }
    ll ans = 0;
   
   
    rep(i, cnts.size()) {
        if (cnts[i] == cntt[i]&&places[i] == placet[i]) continue;
        else ans++;
    }
    cout << ans << endl;
    return 0;
}

    
  

    


    
   
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define p_q priority_queue
#define m_p make_pair
#define pb push_back
#define ld long double
#define F first
#define S second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll qpow(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string iots(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int stoi(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int n,m;
bool g[1111][1111];
int par[2222];
int find(int x)
{
	if(par[x]==x)return x;
	else return par[x]=find(par[x]);
}
void merge(int x,int y)
{
	par[find(x)]=find(y);
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	rep(i,n)
	{
		rep(j,m)
		{
			char c;
			cin>>c;
			if(c=='#')g[i][j]=1;
		}
	}
	g[0][m-1]=g[n-1][m-1]=g[n-1][0]=1;
	rep(i,n+m)par[i]=i;
	rep(i,n)
	{
		rep(j,m)
		{
			if(g[i][j]==1)
			{
				merge(i,n+j);
			}
		}
	}
	set<int>s1,s2;
	rep(i,n)s1.insert(find(i));
	rep(i,m)s2.insert(find(i+n));
	cout<<min(s1.size(),s2.size())-1<<endl;
	return 0;
}
