#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define int long long
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 5e5 + 5 ; 
int n ; 
char s[N], t[N] ; 
void out() {
	puts("-1") ; exit(0) ; 
}
int st[N], top ; 
signed main()
{
	n = gi() ; 
	scanf("%s", s + 1 ) ;
	scanf("%s", t + 1 ) ;
	int a = 0, b = 0 ; 
	rep( i, 1, n ) a += (s[i] == '1') ;
	rep( i, 1, n ) b += (t[i] == '1') ; 
	if( a < b ) out() ;  
	if( (a % 2) != (b % 2) ) out() ; 
	int Ans = 0 ; 
	for(re int i = n; i >= 1; -- i) {
		if( s[i] == '1' ) st[++ top] = i ;
		if( t[i] == '1' ) {
			if(top == 0) out() ; 
			Ans += (st[top] - i), -- top ; 
		}
	}
	for(re int i = 1; i <= top; i += 2) {
		int u = st[i] - st[i + 1] ; 
		Ans += u ; 
	}
	cout << Ans << endl ; 
	return 0 ;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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; }

map<ll, ll> memo;
ll x, y;

ll solve(ll y) {
    if (x >= y) return memo[y] = x-y;
    if (memo.count(y)) return memo[y];
    ll res = y-x;
    if (y % 2 == 0) chmin(res, solve(y/2)+ 1);
    else chmin(res, 1 + min(solve(y+1), solve(y-1)));
    return memo[y] = res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> x >> y;
    cout << solve(y) << endl;
    return 0;
}