#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),i##z=(b);i<=i##z;i++)
#define ROF(i,a,b) for(int i=(a),i##z=(b);i>=i##z;i--)
#define temT template<typename T>
using namespace std;
typedef long long ll;
const int N=int(5e5)+10;

int n; ll res; char s[N],t[N]; bool flag=1;

char rd(){
	char t=getchar();
	while(!isdigit(t)) t=getchar();
	return t;
}

void gn(int &x){//get next
	for(x++;x<=n && s[x]=='0';x++);
}
int k2(int &x){
	int p1,p2;
	p1=x,gn(x);
	p2=x,gn(x);
	res+=p2-p1;
	return (p1<=n && p2>n)?-1:1;
}

int main(){
	int tmp=scanf("%d",&n);
	FOR(i,1,n) s[i]=rd();
	FOR(i,1,n) t[i]=rd();
	int p=0; gn(p);
	FOR(i,1,n) if(t[i]=='1'){
		while(p<i) if(k2(p)<0) flag=0;
		if(p>n) flag=0;
		res+=p-i,gn(p);
	}
	while(p<=n) if(k2(p)<0) flag=0;
	cout<<(flag?res:-1);
	return 0;
}
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;




int main(){

	//ll num = sqrt(HUGE_NUM/2+100);

	//printf("num:%lld\n",num);

	ll N;
	scanf("%lld",&N);

	ll left = 0,right = 1414213562,mid = (left+right)/2;
	ll maxi = -1;

	while(left <= right){
		if((mid*(mid+1)/2) <= N+1){

			maxi = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	printf("%lld\n",N-maxi+1);


	return 0;
}

