#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repll(i,n) for(long long i = 0; i < (long long)(n); i++)


int main(){
    int t;
    cin >> t;
    rep(i, t){
        string s;
        cin >> s;
        int ans = 0;
        if(string(s.size(), 'a') == s) ans = -1;
        else if(s > "atcoder") ans = 0;
        else{
            int k;
            rep(j, s.size()){
                if(s[j] != 'a'){
                    k = j;
                    break;
                }
            }
            if(s[k] > 't') ans = k - 1;
            else ans = k;
        }
        cout << ans << endl;
    }
}
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define ff first
#define ss second
#define rsz resize
#define ins insert 
#define mp make_pair
#define pf push_front 
#define pb push_back
#define eb emplace_back 
#define ft front() 
#define bk back()
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rep(i,a,b) for (int i = (a); i < (b); ++i)

#ifndef ONLINE_JUDGE
#define deb(x) cerr<< #x <<" "; __print(x); cerr<<endl;
#else
#define deb(x)
#endif

const ll mod = 1e9+7; // 998244353;
const ll inf = 1e18; 
const ld pie = acos((ld)-1);

ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void __print(int x) {cerr<<x;}
void __print(char x) {cerr<<x;}
void __print(string x) {cerr<<x;}
void __print(float x) {cerr<<x;}
void __print(double x) {cerr<<x;}
void __print(bool x) {cerr<<x;}
void __print(ll x) {cerr<<x;}

template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";}
template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; }
template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";}
template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";}
template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<typename... T> void read(T&... args) { ((cin>>args),...);}
template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';}



int main(){
    
	int N;
	scanf("%d", &N);

	string s,t;
	cin >> s >> t;

	int cs0 = 0, ct0 = 0;
	rep(i, 0, N) {
		if(s[i] == '0') {
			cs0++;
		}
		if(t[i] == '0') {
			ct0++;
		}
	}

	if(cs0 != ct0) {
		puts("-1");
		return 0;
	}

	int cs = 0, ct = 0;
	int ans = cs0;

	rep(i, 0, N) {
		if(s[i] == '0') {
			cs++;
		}
		if(t[i] == '0') {
			ct++;
		}

		if(s[i] == t[i] && t[i] == '0' && cs == ct) {
			ans--;
		}
	}

	printf("%d\n", ans);

	return 0;

	/*STUFF TO LOOK:	
		1. Check the constraints
		2. Corner cases(n==0||n==1)
		3. Variables in loops
		4. Make sure two ints aren’t multiplied to get a long long
		5. Don't use scanf or printf while using ios_base
		6. using the local variable of the same name when global variable was required to be used
	*/
}
