#include<iostream> 
#include<queue>
using namespace std;
int main()
{
	string s,t;
	long long n , ans=0 , sone=0 , tone=0 , erase=0;	cin>>n>>s>>t;
	queue<int> que;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='1'&&erase>0){
			ans+=i-que.front();
			que.pop();
			erase-- , s[i]='0';
		}			
		if(s[i]=='1') sone++;
		if(t[i]=='1') tone++;
		if(sone>tone){
			s[i]='0' , erase++ , sone--;
			que.push(i);
		}
		else if(sone==tone)
			sone-- , tone--;
	}
	sone=tone=0;
	for(int i=0;i<n;i++)
		if(s[i]=='1') sone++;
	for(int i=0;i<n;i++)
		if(t[i]=='1') tone++;
	if(sone<tone){
		puts("-1");
		return 0;
	}
	while(que.size())
		que.pop();
	for(int i=0;i<n;i++)
	{
		if(t[i]=='1')
			que.push(i);
		if(s[i]=='1'){
			ans+=i-que.front();
			que.pop();
		}
	}
	cout<<ans<<endl;
}
#include <bits/stdc++.h>
#include <time.h>
#define rep(i, n)	for(int i=0;i<(int)(n);i++)
#define ALL(a)	(a).begin(),(a).end()
using namespace std;
using ll=long long;
typedef pair<int,int> P;

bool LOCAL_TEST = false;
int h[30][30],v[30][30];
double priority[30][30];
vector<string> paths;
map<int,char> mp;

inline void InitRand(){
    srand((unsigned int)time(NULL));
}

void read_s_t_a_e(){
    int si,sj,ti,tj,a;
    double e;
    cin>>si>>sj>>ti>>tj>>a>>e;
    string path;
    if(si<=ti) rep(j,ti-si) path+='D';
    else rep(j,si-ti) path+='U';
    if(sj<=tj) rep(j,tj-sj) path+='R';
    else rep(i,sj-tj) path+='L';
    paths.push_back(path);
}

void move(int si,int sj,int ti,int tj,string& path){
    int y=si,x=sj;
    while(y!=ti||x!=tj){
        if(y==ti){
            rep(i,abs(tj-x)) path+=mp[1];
            break;
        }
        else if(x==tj){
            rep(i,abs(ti-y)) path+=mp[0];
            break;
        }
        int next=rand()%2;
        path+=mp[next];
        if(next==0){
            if(y<ti) y++;
            else y--;
        }else{
            if(x<tj) x++;
            else x--;
        }
    }
}

void read_s_t(){
    int si,sj,ti,tj;
    cin>>si>>sj>>ti>>tj;
    string path;
    if(si<=ti) mp[0]='D';
    else mp[0]='U';
    if(sj<=tj) mp[1]='R';
    else mp[1]='L';
    move(si,sj,ti,tj,path);
    cout<<path<<endl;
    flush(cout);
    int a;
    cin>>a;
}

int main(){
    rep(i,30) fill(priority[i],priority[i]+30,-1);
    if(LOCAL_TEST){
        rep(i,30) rep(j,29) cin>>h[i][j];
        rep(i,29) rep(j,30) cin>>v[i][j];
    }
    rep(k,1000){
        if(LOCAL_TEST) read_s_t_a_e();
        else read_s_t();
    }
    if(LOCAL_TEST){
        ofstream output("out.txt");
        rep(k,1000) output<<paths[k]<<'\n';
    }
}