//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			if (!num) {
				num = 1;
				now ^= 1;
				continue;
			}
			//ans = max(ans, num);
			d[++cnt] = num;
			num = 1;
			now ^= 1;
		}

	}
	d[++cnt] = num;

	if (cnt & 1) {
		d[1] += d[cnt];
		cnt--;
	}
	 {
		for (int i = 1; i <= cnt; i++) {
			ans = max(ans, d[i]);
		}
	}

	//ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			ans = max(ans, num);
			num = 1;
			now ^= 1;
		}

	}

	ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0')
			d[++cnt] = 1;
		else
			d[++cnt] = -1;

	}

	int hav = 0;
	for (int i = 1; i <= cnt; i++) {
		hav += d[i];
		if (hav < 0)
			hav = 0;
		ans = max(ans, hav);
	}
	int res = 0;
	hav = 0;
	for (int i = 1; i <= cnt; i++) {
		hav += d[i];
		if (hav > 0)
			hav = 0;
		res = min(res, hav);
	}

	//ans = max(ans, num);

	cout << max(ans, -res) << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			if (!num) {
				num = 1;
				now ^= 1;
				continue;
			}
			//ans = max(ans, num);
			d[++cnt] = num;
			num = 1;
			now ^= 1;
		}

	}
	d[++cnt] = num;

	if (cnt & 1) {
		int res = 0;
		int _min = 1e6;
		for (int i = 1; i <= cnt; i++) {
			res = max(res, d[i]);
			if (i & 1)
				_min = min(_min, d[i]);
		}
		ans = res + _min;
		/*res = 0;
		for (int i = 2; i <= cnt; i++) {
			res = max(res, d[i]);
		}
		ans = min(ans, res + d[1]);*/
	}
	else {
		for (int i = 1; i <= cnt; i++) {
			ans = max(ans, d[i]);
		}
	}

	ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			if (!num) {
				num = 1;
				now ^= 1;
				continue;
			}
			//ans = max(ans, num);
			d[++cnt] = num;
			num = 1;
			now ^= 1;
		}

	}

	if (cnt & 1) {
		int res = 0;
		for (int i = 1; i < cnt; i++) {
			res = max(res, d[i]);
		}
		ans = res + d[cnt];
		res = 0;
		for (int i = 2; i <= cnt; i++) {
			res = max(res, d[i]);
		}
		ans = min(ans, res + d[1]);
	}
	else {
		for (int i = 1; i <= cnt; i++) {
			ans = max(ans, d[i]);
		}
	}

	ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0')
			d[++cnt] = 1;
		else
			d[++cnt] = -1;

	}

	int hav = 0;
	for (int i = 1; i <= cnt; i++) {
		hav += d[i];
		if (hav < 0)
			hav = 0;
		ans = max(ans, abs(hav));
	}

	//ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			if (!num) {
				num = 1;
				now ^= 1;
				continue;
			}
			//ans = max(ans, num);
			d[++cnt] = num;
			num = 1;
			now ^= 1;
		}

	}
	d[++cnt] = num;

	if (cnt & 1) {
		d[1] += d[cnt];
		cnt--;
	}
	 {
		for (int i = 1; i <= cnt; i++) {
			ans = max(ans, d[i]);
		}
	}

	ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0' + now) {
			num++;
		}
		else {
			if (!num) {
				num = 1;
				now ^= 1;
				continue;
			}
			//ans = max(ans, num);
			d[++cnt] = num;
			num = 1;
			now ^= 1;
		}

	}
	d[++cnt] = num;

	if (cnt & 1) {
		int res = 0;
		for (int i = 1; i < cnt; i++) {
			res = max(res, d[i]);
		}
		ans = res + d[cnt];
		res = 0;
		for (int i = 2; i <= cnt; i++) {
			res = max(res, d[i]);
		}
		ans = min(ans, res + d[1]);
	}
	else {
		for (int i = 1; i <= cnt; i++) {
			ans = max(ans, d[i]);
		}
	}

	ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define pf push_front
#define INF 0x3f3f3f3f
#define MAXN (int) 1e6 + 7
#define all(a) a.begin(), a.end()

typedef long long ll;

const ll mod = 1e9 + 7;
const double PI = acos(-1);

using namespace std;

int N, M, K, T;

int d[MAXN];

signed main(void) {

#ifdef Sunshine
	freopen("data.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;
	string s;
	cin >> s;
	string t;
	cin >> t;

	int now = 0;
	int num = 0;

	int ans = 0;

	int a, b;
	a = b = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == '1') {
			a++;
		}
		if (t[i] == '1')
			a--;
		if (t[i] == '0')
			b++;
		if (s[i] == '0')
			b--;
	}

	if (a || b) {
		return cout << -1 << '\n', 0;
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (s[i] == t[i])
			continue;
		if (s[i] == '0')
			d[++cnt] = 1;
		else
			d[++cnt] = -1;

	}

	int hav = 0;
	for (int i = 1; i <= cnt; i++) {
		hav += d[i];
		ans = max(ans, abs(hav));
	}

	//ans = max(ans, num);

	cout << ans << '\n';

#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
#define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

const int N = 1000005;
string a,b;
void test_case(){
	int n,i;
	cin>>n;
	cin>>a;
	cin>>b;
	vector<int>v;
	int cnt1 = 0;
	int cnt2 = 0;
	for(i=0;i<n;i++){
		if(a[i] != b[i]) v.pb(a[i]-'0');
		if(a[i] == '1') cnt1++;
		if(b[i] == '1') cnt2++;
	}
	if(cnt1 != cnt2){
		cout<<"-1\n";
		return ;
	}
	int cnt = 0;
	int mx = 0;
	for(i=0;i<v.size()-1;i++){
		cnt++;
		if(v[i] != v[i+1]){
			mx = max(mx,cnt);
			cnt = 0;
		}
	}
	mx = max(mx,cnt);
	cout<<mx<<"\n";
    return ;
}
signed main(){
    IOS;
    int t;
    t = 1;
    //~ s(t);
    //~ cin>>t;
    //~ int tm = 0;
    while(t--){
         //~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
//~ #define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

void test_case(){
	int n,i,j;
	string a,b,a1,a2;
	cin>>n;
	cin>>a1;
	cin>>a2;
	a = "";
	b = "";
	int zero = 0;
	int one = 0;
	for(i=0;i<n;i++){
		if(a1[i] != a2[i]){
			a.pb(a1[i]);
			if(a1[i] == '0') zero++;
			if(a1[i] == '1') one++;
			b.pb(a2[i]);
		}
	}
	if(zero != one){
		cout<<"-1\n";
		return ;
	}
	int ans = 0;
	while(true){
		n = a.length();
		if(n == 0) break ;
		i = n-1;
		j = n-1;
		string tmp = "";
		while(j >= 0 && i >= 0){
			while(i >= 0 && b[j] != a[i]){
				tmp.pb(a[i]);
				i--;
				ans++;
			}
			while(i >= 0 && b[j] == a[i]){
				i--;
				j--;
			}
		}
		reverse(all(tmp));
		a = tmp;
	}
	cout<<ans<<"\n";
	return ;
}
signed main(){
	//~ IOS;
	int t;
	t = 1;
	//~ s(t);
	//~ preprocess();
	//~ cin>>t;
	//~ int tm = 0;
    while(t--){
 		//~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
#define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

const int N = 1000005;
string a,b;
void test_case(){
	int n,i;
	cin>>n;
	cin>>a;
	cin>>b;
	vector<int>v;
	int cnt1 = 0;
	int cnt2 = 0;
	for(i=0;i<n;i++){
		if(a[i] != b[i]) v.pb(a[i]-'0');
		if(a[i] == '1') cnt1++;
		if(b[i] == '1') cnt2++;
	}
	if(cnt1 != cnt2){
		cout<<"-1\n";
		return ;
	}
	int su = 0;
	int mn = 0;
	int mx = 0;
	for(auto k: v){
		if(k == 0) k = -1;
		su += k;
		mn = min(mn,su);
		mx = max(mx,su);
	}
	cout<<mx-mn<<"\n";
    return ;
}
signed main(){
    IOS;
    int t;
    t = 1;
    //~ s(t);
    //~ cin>>t;
    //~ int tm = 0;
    while(t--){
         //~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
//~ #define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

void test_case(){
	int n,i,j;
	string a,b,a1,a2;
	//~ cin>>n;
	cin>>a1;
	cin>>a2;
	a = "";
	b = "";
	int zero = 0;
	int one = 0;
	for(i=0;i<(int)a1.length();i++){
		if(a1[i] != a2[i]){
			a.pb(a1[i]);
			if(a1[i] == '0') zero++;
			if(a1[i] == '1') one++;
			b.pb(a2[i]);
		}
	}
	if(zero != one){
		cout<<"-1\n";
		return ;
	}
	int ans = 0;
	while(true){
		n = a.length();
		if(n == 0) break ;
		i = n-1;
		j = n-1;
		string tmp = "";
		while(j >= 0 && i >= 0){
			while(i >= 0 && b[j] != a[i]){
				tmp.pb(a[i]);
				i--;
				ans++;
			}
			while(i >= 0 && b[j] == a[i]){
				i--;
				j--;
			}
		}
		reverse(all(tmp));
		a = tmp;
		int n1 = a.length();
		a1 = "";
		a2 = "";
		for(i=0;i<n1;i++){
			if(a[i] != b[i]){
				a1.pb(a[i]);
				a2.pb(b[i]);
			}
		}
		a = a1;
		b = a2;
	}
	cout<<ans<<"\n";
	return ;
}
signed main(){
	//~ IOS;
	int t;
	t = 1;
	//~ s(t);
	//~ preprocess();
	//~ cin>>t;
	//~ int tm = 0;
    while(t--){
 		//~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
//~ #define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

void test_case(){
	int n,i,j;
	string a,b,a1,a2;
	cin>>n;
	cin>>a1;
	cin>>a2;
	a = "";
	b = "";
	int zero = 0;
	int one = 0;
	for(i=0;i<(int)a1.length();i++){
		if(a1[i] != a2[i]){
			a.pb(a1[i]);
			if(a1[i] == '0') zero++;
			if(a1[i] == '1') one++;
			b.pb(a2[i]);
		}
	}
	if(zero != one){
		cout<<"-1\n";
		return ;
	}
	int ans = 0;
	while(true){
		n = a.length();
		if(n == 0) break ;
		i = n-1;
		j = n-1;
		string tmp = "";
		while(j >= 0 && i >= 0){
			while(i >= 0 && b[j] != a[i]){
				tmp.pb(a[i]);
				i--;
				ans++;
			}
			while(i >= 0 && b[j] == a[i]){
				i--;
				j--;
			}
		}
		reverse(all(tmp));
		a = tmp;
		int n1 = a.length();
		a1 = "";
		a2 = "";
		for(i=0;i<n1;i++){
			if(a[i] != b[i]){
				a1.pb(a[i]);
				a2.pb(b[i]);
			}
		}
		a = a1;
		b = a2;
	}
	cout<<ans<<"\n";
	return ;
}
signed main(){
	//~ IOS;
	int t;
	t = 1;
	//~ s(t);
	//~ preprocess();
	//~ cin>>t;
	//~ int tm = 0;
    while(t--){
 		//~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
//~ #define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

void test_case(){
	int n,i,j;
	string a,b,a1,a2;
	cin>>n;
	cin>>a1;
	cin>>a2;
	a = "";
	b = "";
	int zero = 0;
	int one = 0;
	for(i=0;i<n;i++){
		if(a1[i] != a2[i]){
			a.pb(a1[i]);
			if(a1[i] == '0') zero++;
			if(a1[i] == '1') one++;
			b.pb(a2[i]);
		}
	}
	if(zero != one){
		cout<<"-1\n";
		return ;
	}
	n = a.length();
	i = n-1;
	j = n-1;
	int ans = 0;
	while(j >= 0 && i >= 0){
		while(i >= 0 && b[j] != a[i]){
			i--;
			ans++;
		}
		while(i >= 0 && b[j] == a[i]){
			i--;
			j--;
		}
	}
	cout<<ans<<"\n";
	return ;
}
signed main(){
	//~ IOS;
	int t;
	t = 1;
	//~ s(t);
	//~ preprocess();
	//~ cin>>t;
	//~ int tm = 0;
    while(t--){
 		//~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
#define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

void test_case(){
	int n,i;
	cin>>n;
	string a,b;
	cin>>a;
	cin>>b;
	vector<int>v;
	int cnt1 = 0;
	int cnt2 = 0;
	for(i=0;i<n;i++){
		if(a[i] != b[i]) v.pb(a[i]-'0');
		if(a[i] == '1') cnt1++;
		if(b[i] == '1') cnt2++;
	}
	if(cnt1 != cnt2){
		cout<<"-1\n";
		return ;
	}
	int cnt = 0;
	int mx = 0;
	for(i=0;i<v.size()-1;i++){
		cnt++;
		if(v[i] != v[i+1]){
			mx = max(mx,cnt);
			cnt = 0;
		}
	}
	mx = max(mx,cnt);
	cout<<mx<<"\n";
    return ;
}
signed main(){
    IOS;
    int t;
    t = 1;
    //~ s(t);
    //~ cin>>t;
    //~ int tm = 0;
    while(t--){
         //~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define p(k) printf("%d ",k)
#define pl(k) printf("%lld ",k)
#define pn(k) printf("%d\n",k)
#define pln(k) printf("%lld\n",k)
#define ps(str) printf("%s",str)
#define s(k) scanf("%d",&k)
#define sl(k) scanf("%lld",&k)
#define ss(str) scanf("%s",str)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ft first
#define sd second
#define int long long
#define M 1000000007
#define ppi pair<int,int>
#define count(x) __builtin_popcountll(x)

//~ For Indexed_set --
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ using namespace __gnu_pbds;
//~ #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

const int N = 1000005;
string a,b;
void test_case(){
	int n,i;
	cin>>n;
	cin>>a;
	cin>>b;
	vector<int>v;
	int cnt1 = 0;
	int cnt2 = 0;
	for(i=0;i<n;i++){
		if(a[i] != b[i]) v.pb(a[i]-'0');
		if(a[i] == '1') cnt1++;
		if(b[i] == '1') cnt2++;
	}
	if(cnt1 != cnt2){
		cout<<"-1\n";
		return ;
	}
	int cnt = 0;
	int mx = 0;
	for(i=0;i<(int)v.size()-1;i++){
		cnt++;
		if(v[i] != v[i+1]){
			mx = max(mx,cnt);
			cnt = 0;
		}
	}
	mx = max(mx,cnt);
	cout<<mx<<"\n";
    return ;
}
signed main(){
    IOS;
    int t;
    t = 1;
    //~ s(t);
    //~ cin>>t;
    //~ int tm = 0;
    while(t--){
         //~ cout<<"Case #"<<++tm<<": ";
        test_case();
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
	int n;
	string s, t;
	cin >> n;
	cin.get();
	getline(cin, s);
	getline(cin, t);
	set<int> a, b;
	for (int i = 0; i < n; i++)
	{
		if (s[i] != t[i])
			s[i] == '0' ? a.insert(i) : b.insert(i);
	}
	if (a.size() != b.size())
	{
		cout << -1;
		return 0;
	}

	int count = 0;
	while (!a.empty() && !b.empty())
	{
		auto a_item = (*a.begin());
		a.erase(a.begin());
		auto b_item = (*b.begin());
		b.erase(b.begin());
		while (true)
		{
			if (a_item < b_item)
			{
				auto m = a.upper_bound(b_item);
				if (m == a.end())
					break;
				a_item = (*m);

				auto w = b.upper_bound(a_item);
				if (w == b.end())
					break;
				b_item = (*w);

				a.erase(m);
				b.erase(w);
			}
			else
			{
				auto m = b.upper_bound(a_item);
				if (m == b.end())
					break;
				b_item = (*m);

				auto w = a.upper_bound(b_item);
				if (w == a.end())
					break;
				a_item = (*w);

				b.erase(m);
				a.erase(w);
			}
		}
		count++;
	}
	cout << count;
	return 0;
}


#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>

using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);

	int n;

	scanf("%d", &n);

	string s, t;

	char input[1000001];

	scanf("%s", input);
	s = input;
	scanf("%s", input);
	t = input;

	int cnt[2] = { 0, };

	for (int i = 0; i < n; i++)
	{
		cnt[s[i] - '0']++;
		cnt[t[i] - '0']--;
	}

	if (cnt[0])
	{
		printf("-1\n");

		return 0;
	}

	int sol = 0;

	for (int i = 0; i < n; i++)
	{
		if (s[i] != t[i])
		{
			int num = s[i] - '0';

			if (!cnt[!num])
			{
				sol++;
			}

			else
			{
				cnt[!num]--;
			}

			cnt[num]++;
		}
	}

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

	return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#pragma warning(disable:4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;

char str0[1000005];
char str1[1000005];

void solve()
{
    int n;
    scanf("%d%s%s", &n, str0, str1);
    vector<int> v;
    int i;
    int cnt0 = 0, cnt1 = 0;
    for (i = 0; i < n; i++) {
        if (str0[i] != str1[i]) {
            int tmp = str0[i] - '0';
            if (tmp == 0) {
                cnt0++;
            }
            else {
                cnt1++;
            }
            v.push_back(tmp);
        }
    }
    if (cnt0 != cnt1) {
        printf("-1\n"); return;
    }
    int siz = (int)v.size();
    int cnt = 0;
    int max = 0;
    vector<int> v0;
    for (i = 0; i < siz; i++) {
        if (i == 0 || v[i-1]==v[i]) {
            cnt++;
            max = MAX(max, cnt);
        }
        else {
            v0.push_back(cnt);
            cnt = 1;
            max = MAX(max, cnt);
        }
    }
    v0.push_back(cnt);
    if ((int)v0.size() % 2 == 1 && (int)v0.size() > 2) {
        max = MAX(max, v0[0] + v0[(int)v0.size() - 1]);
    }

    printf("%d\n", max);
    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#pragma warning(disable:4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;

char str0[1000005];
char str1[1000005];

void solve()
{
    int n;
    scanf("%d%s%s", &n, str0, str1);
    vector<int> v;
    int i;
    int cnt0 = 0, cnt1 = 0;
    for (i = 0; i < n; i++) {
        if (str0[i] != str1[i]) {
            int tmp = str0[i] - '0';
            if (tmp == 0) {
                cnt0++;
            }
            else {
                cnt1++;
            }
            v.push_back(tmp);
        }
    }
    if (cnt0 != cnt1) {
        printf("-1\n"); return;
    }
    int siz = (int)v.size();
    if (siz == 0) {
        printf("0\n"); return;
    }
    int curr = 0;
    int min = INF, max = -INF;
    for (i = 0; i < siz; i++) {
        if (v[i] == 0) {
            curr++;
        }
        else {
            curr--;
        }
        min = MIN(min, curr);
        max = MAX(max, curr);
    }
    printf("%d\n", max - min);
    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#pragma warning(disable:4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;

char str0[1000005];
char str1[1000005];

void solve()
{
    int n;
    scanf("%d%s%s", &n, str0, str1);
    vector<int> v;
    int i;
    int cnt0 = 0, cnt1 = 0;
    for (i = 0; i < n; i++) {
        if (str0[i] != str1[i]) {
            int tmp = str0[i] - '0';
            if (tmp == 0) {
                cnt0++;
            }
            else {
                cnt1++;
            }
            v.push_back(tmp);
        }
    }
    if (cnt0 != cnt1) {
        printf("-1\n"); return;
    }
    int siz = (int)v.size();
    int cnt = 0;
    int max = 0;
    for (i = 0; i < siz; i++) {
        if (i == 0 || v[i-1]==v[i]) {
            cnt++;
            max = MAX(max, cnt);
        }
        else {
            cnt = 1;
            max = MAX(max, cnt);
        }
    }
    printf("%d\n", max);
    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#pragma warning(disable:4996)

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
//const long long MOD = 1000000007;
const long long MOD = 998244353;

using namespace std;

char str0[1000005];
char str1[1000005];

void solve()
{
    int n;
    scanf("%d%s%s", &n, str0, str1);
    vector<int> v;
    int i;
    int cnt0 = 0, cnt1 = 0;
    for (i = 0; i < n; i++) {
        if (str0[i] != str1[i]) {
            int tmp = str0[i] - '0';
            if (tmp == 0) {
                cnt0++;
            }
            else {
                cnt1++;
            }
            v.push_back(tmp);
        }
    }
    if (cnt0 != cnt1) {
        printf("-1\n"); return;
    }
    int siz = (int)v.size();
    int curr = 0;
    int min = INF, max = -INF;
    for (i = 0; i < siz; i++) {
        if (v[i] == 0) {
            curr++;
        }
        else {
            curr--;
        }
        min = MIN(min, curr);
        max = MAX(max, curr);
    }
    printf("%d\n", max - min);
    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


#include<iostream>
#include<vector>
#include<stack>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<tuple>
#include<bitset>
#include<queue>
#include<random>
using namespace std;
int gcd(int i, int j) {
    if (j == 0)return i;
    else return gcd(j, i % j);
}
template<typename T> inline T getint() {
    T val = 0;
    char c;

    bool neg = false;
    while ((c = getchar()) && !(c >= '0' && c <= '9')) {
        neg |= c == '-';
    }

    do {
        val = (val * 10) + c - '0';
    } while ((c = getchar()) && (c >= '0' && c <= '9'));

    return val * (neg ? -1 : 1);
}
const long long INF = 10000000;
const int Y = 1100;
const long long mod = 1000000007;
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int c1 = 0;
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1')++c1;
        if (t[i] == '1')--c1;
    }
    if (c1 != 0) {
        cout << -1;
        return 0;
    }
    string s1 = "", t1 = "";
    for (int i = 0; i < n; ++i) {
        if (s[i] != t[i]) {
            s1.push_back(s[i]);
            t1.push_back(t[i]);
        }
    }
    int l1 = 0, l2 = 0;
    n = s1.length();
    int ans = 0;
    int b = 0;
    for (int i = 0; i < n; ++i) {
        if (s1[i] == '1')++b;
        else --b;
        l1 = max(l1, b);
        l2 = min(l2, b);
    }
    ans = l1 - l2;
    cout << ans << endl;
    return 0;
}
//freopen("painter.in", "r", stdin);
//freopen("painter.out", "w", stdout);
/*
3
ooo
o..
o..
*/
/* Fri May 15 2020 19:54:42 GMT+0300 (MSK) */

/* Wed May 20 2020 19:33:48 GMT+0300 (MSK) */


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>
#include <cmath>
#include <iterator>
#include <cstdint>
#include <list>
#include <memory>
#include <unordered_set>
#include <sstream>
#include <cassert>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>
#define ll long long
#define int long long
using namespace std;
int a[2000000];
void main()
{
#if defined _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout << fixed << setprecision(7);
	int n;
	cin >> n;
	string st,st1;
	cin >> st >> st1;
	int k1 = 0;
	int k2 = 0;
	int k = 0;
	int t = 0;
	int in = 0;
	for (int i = 0; i < n; i++)
	{
		if (st[i] != st1[i])
		{
			if (st[i] == '1')
			{
				a[in] = 1;
				in++;
				k1++;
			}
			else
			{
				a[in] = -1;
				in++;
				k2++;
			}
		}
	}
	if (k1 == k2)
	{
		int msumm = 0;
		int cur = 0;
		for (int i = 0; i < in; i++)
		{
			cur += a[i];
			if (cur < 0)
			{
				cur = 0;
			}
			msumm = max(cur, msumm);
		}
		cur = 0;
		for (int i = 0; i < in; i++)
		{
			cur -= a[i];
			if (cur < 0)
			{
				cur = 0;
			}
			msumm = max(cur, msumm);
		}
		cout << msumm;
	}
	else
	{
		cout << -1;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>
#include <cmath>
#include <iterator>
#include <cstdint>
#include <list>
#include <memory>
#include <unordered_set>
#include <sstream>
#include <cassert>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>
#define ll long long
#define int long long
using namespace std;
int a[1000000];
void main()
{
#if defined _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout << fixed << setprecision(7);
	int n;
	cin >> n;
	string st,st1;
	cin >> st >> st1;
	int k1 = 0;
	int k2 = 0;
	int k = 0;
	int t = 0;
	char pr = '2';
	for (int i = 0; i < n; i++)
	{
		if (st[i] != st1[i])
		{
			if (pr == st[i])
			{
				t++;
			}
			else
			{
				k = max(k, t);
				t = 1;
				pr = st[i];
			}
			if (st[i] == '1')
			{
				k1++;
			}
			else
			{
				k2++;
			}
		}
	}
	if (k1 == k2)
	{
		cout << max(k, t);
	}
	else
	{
		cout << -1;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>
#include <cmath>
#include <iterator>
#include <cstdint>
#include <list>
#include <memory>
#include <unordered_set>
#include <sstream>
#include <cassert>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>
#define ll long long
#define int long long
using namespace std;
int a[1000000];
void main()
{
#if defined _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout << fixed << setprecision(7);
	int n;
	cin >> n;
	string st,st1;
	cin >> st >> st1;
	int k1 = 0;
	int k2 = 0;
	int k = 0;
	int t = 0;
	for (int i = 0; i < n; i++)
	{
		if (st[i] != st1[i])
		{
			if (st[i] == '1')
			{
				t++;
				k1++;
			}
			else
			{
				t--;
				k2++;
			}
			k = max(k, abs(t));
		}
	}
	if (k1 == k2)
	{
		cout << k;
	}
	else
	{
		cout << -1;
	}
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <list>
#include <math.h>
#include <bitset>
#include <iterator>
#include <map>
#include <iomanip>
#include <functional>
#include <string>
#include <algorithm>
#include <queue>
#include <random>
#include <numeric>
#include <set>
#include <cassert>
#include <cmath>
#include <cinttypes>

using namespace std;

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; }
template<class T> istream& operator>>(istream& is, vector<T>& v) { for (auto i = begin(v); i != end(v); i++) is >> *i; return is; }
struct fast_io {
	fast_io& operator>> (int& target) { scanf("%" PRId32, &target);  return *this; }
	fast_io& operator>> (unsigned int& target) { scanf("%" PRIu32, &target);  return *this; }
	fast_io& operator>> (long long& target) { scanf("%" PRId64, &target);  return *this; }
	fast_io& operator>> (unsigned long long& target) { scanf("%" PRIu64, &target);  return *this; }
	template<typename T> fast_io& operator>> (vector<T>& target) { for (auto& value : target) { *this >> value; } return *this; }
	fast_io& operator<< (const int value) { printf("%" PRId32, value); return *this; }
	fast_io& operator<< (const unsigned int value) { printf("%" PRIu32, value); return *this; }
	fast_io& operator<< (const long long value) { printf("%" PRId64, value); return *this; }
	fast_io& operator<< (const unsigned long long value) { printf("%" PRIu64, value); return *this; }
	fast_io& operator<< (const string& value) { printf("%s", value.c_str()); return *this; }
	template<typename T> fast_io& operator<< (const vector<T>& target) { for (auto& value : target) { *this << value << " "; } return *this; }
}; fast_io fio;

template<class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last, UnaryPredicate predicate) {
	ForwardIt it;
	typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last);
	while (count > 0) {
		it = first;	step = count / 2;
		std::advance(it, step);
		if (!predicate(*it)) { first = ++it; count -= step + 1; }
		else count = step;
	}
	return first;
}

#define MOD 1000000007
#define pii pair<int,int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n;
	cin >> n;
	string s, t;
	cin >> s >> t;

	set<int> pos0, pos1;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == t[i]) continue;
		if (s[i] > t[i])
		{
			pos1.insert(i);
		}
		else
		{
			pos0.insert(i);
		}
	}

	if (pos1.size() != pos0.size())
	{
		cout << -1;
		return 0;
	}
	int ans = 0;
	while (pos0.size() > 0)
	{
		if (*pos0.begin() > * pos1.begin())
			swap(pos0, pos1);
		ans++;
		int pos = *pos0.begin();
		while (pos0.size() > 0 && pos1.size() > 0)
		{
			auto tt = pos0.lower_bound(pos);
			if (tt == pos0.end())
				break;
			auto t = pos1.lower_bound(*tt);
			if (t == pos1.end())
			{
				break;
			}
			pos = *t;
			pos0.erase(tt);
			pos1.erase(t);
		}

	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

string s, t;
int n;

int main() {
	cin >> n;
	cin >> s >> t;
	if (s == t) {
		cout << '0';
		return 0;
	}
	else {
		int zeronumS = 0;
		int zeronumT = 0;
		for (int i = 0; i < n; i++) {
			if (s[i] == '0') {
				zeronumS++;
			}
			if (t[i] == '0') {
				zeronumT++;
			}
		}
		if (zeronumS != zeronumT) {
			cout << -1;
			return 0;
		}
		else {
			string wrongsymbol = "";
			for (int i = 0; i < n; i++) {
				if (s[i] != t[i]) {
					wrongsymbol = wrongsymbol + s[i];
				}
			}
			int cnt = 1;
			int max = 1;
			for (int i = 1; i < wrongsymbol.length(); i++) {
				if (wrongsymbol[i] == wrongsymbol[i - 1]) {
					cnt++;
					if (cnt > max) {
						max = cnt;
					}
				}
				else {
					cnt = 1;
				}
			}
			cout << max;
		}
	}
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int n, ans;
string s, t;
int Array[1000005];

int max(int a, int b) {
	if (a > b) {
		return a;
	}
	else {
		return b;
	}
}

int get(int x) {
	int cur = 0;
	int mx = 0;
	for (int i = 0; i < n; i++) {
		cur = cur + (x * Array[i]);
		mx = max(mx, cur);
		if (cur < 0) {
			cur = 0;
		}
	}
	return mx;
}

int main() {
	cin >> n;
	cin >> s >> t;
	int cnt1 = 0;
	int cnt2 = 0;
	for (int i = 0; i < n; i++) {
		if (s[i] == '0') {
			cnt1++;
		}
		if (t[i] == '0') {
			cnt2++;
		}
	}
	if (cnt1 != cnt2) {
		cout << -1;
		return 0;
	}
	for (int i = 0; i < n; i++) {
		if (s[i] != t[i]) {
			if (s[i] == '1') {
				Array[i] = 1;
			}
			else {
				Array[i] = -1;
			}
		}
	}
	ans = max(get(1), get(-1));
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <assert.h>
#include <map>
#include <set>
#include <bitset>
#include <iomanip>
#include <stack>
// #include <unordered_map>
#include <cmath>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
// #define INF 0x7f7f7f7f  //2139062143
#define INF1 0x3f3f3f3f //1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.14159265358979323846264338327950254
#define pb push_back
#define ll long long
#define debug cout << "debug\n";
#define STDIN                      \
    freopen("in.txt", "r", stdin); \
    freopen("out.txt", "w", stdout);
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m, n) for (int i = m; i < n; i++)
#define PII pair<int, int>
#define PLL pair<long, long>
#define ft first
#define sd second
#define pb push_back
#define ST(x, y, z) sort(x + y, x + z);
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define rrep(i, a, b) for (register int i = a; i >= b; i--)
#define PIII pair<int, PII>
#define re read()
#define case  \
    int T;    \
    cin >> T; \
    while (T--)
// #define int ll
#define LL long long
#define int ll
inline int read()
{
    char ch = getchar();
    int x = 0, f = 0;
    while (ch < '0' || ch > '9')
        f |= ch == '-', ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return f ? -x : x;
}

template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

const int N = 1e6 + 10;
char s1[N], s2[N];
int dp[N];
signed main()
{
    // STDIN
    int n; n = re;
    scanf("%s %s", s1 + 1, s2 + 1);
    int one1 = 0;
    int one2 = 0;
    rep(i, 1, n)
    {
        if (s1[i] == '1') one1++;
    }
    rep(i, 1, n)
    {
        if (s2[i] == '1') one2++;
    }
    if (one1!= one2) {
        cout << -1 << endl;
        return 0;
    }

    int onef = 0, zerof = 0;
    int ans = 0;
    rep(i, 1, n)
    {
        if (s1[i] != s2[i] && s1[i] == '1')
        {
            if (zerof <= 0)
            {
                ans++;
                onef++;
            }
            else
            {
                zerof--;
                onef++;
            }

        }
        else if (s1[i] != s2[i] && s1[i] == '0'){
            if (onef <= 0)
            {
                ans++;
                zerof++;
            }
            else{
                onef--;
                zerof++;
            }
        }
    }
    cout << ans << endl;

}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef tuple<int, int, int> TIII;
typedef vector<int> VI;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int MR = 1e6 + 10;

char s[MR], t[MR];

int main()
{
    fast_io();

    int n;
    scanf("%d%s%s", &n, s, t);

    vector<int> ile = { 0,0 };
    string tmp;
    REP(i, n)
        if (s[i] != t[i])
        {
            tmp += s[i];
            ile[s[i] - '0']++;
        }

    if (ile[0] != ile[1])
    {
        printf("-1\n");
        return 0;
    }

    int res = 0;
    ile[0] = ile[1] = 0;
    REP(i, tmp.length())
    {
        if (tmp[i] == '0')
        {
            if (ile[1])
            {
                ile[1]--;
                ile[0]++;
            }
            else
            {
                ile[0]++;
                res++;
            }
        }
        else
        {
            if (ile[0])
            {
                ile[0]--;
                ile[1]++;
            }
            else
            {
                ile[1]++;
                res++;
            }
        }
    }

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

    return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef tuple<int, int, int> TIII;
typedef vector<int> VI;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int MR = 1e6 + 10;

char s[MR], t[MR];

int main()
{
    fast_io();

    int n;
    scanf("%d%s%s", &n, s, t);

    vector<int> ile = { 0,0 };
    string tmp;
    REP(i, n)
        if (s[i] != t[i])
        {
            tmp += s[i];
            ile[s[i] - '0']++;
        }

    if (ile[0] != ile[1])
    {
        printf("-1\n");
        return 0;
    }

    int res = 0;
    for (int i = 0; i < tmp.length();)
    {
        int j = i + 1;
        while (j < tmp.length() && tmp[i] == tmp[j])j++;

        res = max(res, j - i);
        i = j;
    }

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

    return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int dp[1000010];
int mx(int sign,int n) {
    int val = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        val += (dp[i]) * sign;
        ans = max(ans, val);
        if (val < 0) {
            val = 0;
        }
    }
    return ans;
}
int main() {
    //R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnts[2] = {0,0};
    r(a.size()) {
        if (a[i] != b[i]) cnts[a[i] - '0']++, dp[i] = 2 * (a[i] - '0') - 1;
        else dp[i] = 0;
    }
    if (cnts[0] != cnts[1]) {
        cout << -1 << endl;
    }
    else {
        cout << max(mx(1, n), mx(-1, n))<<endl;
    }
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
    //R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnt_0=0,cnt_1=0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) a[i] == '0' ? cnt_0++ : cnt_1++;
    }
    if (cnt_0 != cnt_1) {
        cout << -1 << endl;
    }else{
        int f = -1;
        int current = 0;
        int ans = 0;
        int rem = 0;
        for (int i = 0; i < a.size(); i++) {
            if (a[i] != b[i]) {
                int start = i;
                int runner = i + 1;
                int current = i;
                while (runner < a.size() && a[runner]==b[current]) {
                    runner++;
                    current++;
                }
                if (i == current) {
                    rem++;
                }
                else {
                    ans++;
                }
                i = current;
            }
        }
        ans += rem / 2;
        cout << ans << endl;
    }

    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
   // R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnt_0=0,cnt_1=0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) a[i] == '0' ? cnt_0++ : cnt_1++;
    }
    if (cnt_0 != cnt_1) {
        cout << -1 << endl;
    }else{
        int f = -1;
        int current = 0;
        int ans = 0;
        for (int i = 0; i < a.size(); i++) {
            if (a[i] != b[i]) {
                if (f == -1) {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1]) {
                        f = i;
                        i++;
                        current++;
                    }
                }
                else {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1]) {
                        i++;
                        current++;
                    }
                    else {
                        cnt_0 -= current;
                        cnt_1 -= current;
                        ans++;
                        current = 0;
                        f = -1;
                    }
                }
            }
            else {
                cnt_0 -= current;
                cnt_1 -= current;
                current = 0;
                f = -1;
            }
        }
        if (f != -1 && current) {
            ans++;
        }
        ans += cnt_0- current;
        cout << ans << endl;
    }

    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
    //R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnt=0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) cnt++;
    }
    if (cnt % 2 == 1)
    {
        cout << -1 << endl;
    }
    else {
        cout << cnt / 2 << endl;
    }
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
    //R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnt_0=0,cnt_1=0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) a[i] == '0' ? cnt_0++ : cnt_1++;
    }
    if (cnt_0 != cnt_1) {
        cout << -1 << endl;
    }else{
        int f = -1;
        int current = 0;
        int ans = 0;
        for (int i = 0; i < a.size(); i++) {
            if (a[i] != b[i]) {
                if (f == -1) {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1] && a[i]!=a[i+1]) {
                        f = i;
                        i++;
                        current++;
                    }
                }
                else {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1] && a[i] != a[i + 1]) {
                        i++;
                        current++;
                    }
                    else {
                        cnt_0 -= current;
                        cnt_1 -= current;
                        ans++;
                        current = 0;
                        f = -1;
                    }
                }
            }
            else {
                if (f != -1 && current) {
                    cnt_0 -= current;
                    cnt_1 -= current;
                    ans++;
                    current = 0;
                    f = -1;
                }
            }
        }
        if (f != -1 && current) {
            ans++;
        }
        ans += cnt_0- current;
        cout << ans << endl;
    }

    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<=t;i++)
#define r(e) for(int i=0;i<e;i++)
#define rr(i,j,I,J) for(int i=0;i<I;i++)for(int j=0;j<J;j++)
#define rr1(i,j,I,J) for(int i=1;i<=I;i++)for(int j=1;j<=J;j++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
int main() {
    //R_W;
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int cnt_0=0,cnt_1=0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) a[i] == '0' ? cnt_0++ : cnt_1++;
    }
    if (cnt_0 != cnt_1) {
        cout << -1 << endl;
    }else{
        int f = -1;
        int current = 0;
        int ans = 0;
        for (int i = 0; i < a.size(); i++) {
            if (a[i] != b[i]) {
                if (f == -1) {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1] && a[i]!=a[i+1]) {
                        f = i;
                        i++;
                        current++;
                    }
                }
                else {
                    if (i + 1 < a.size() && a[i + 1] != b[i + 1] && a[i] != a[i + 1]) {
                        i++;
                        current++;
                    }
                    else {
                        cnt_0 -= current;
                        cnt_1 -= current;
                        ans++;
                        current = 0;
                        f = -1;
                    }
                }
            }
            else {
                cnt_0 -= current;
                cnt_1 -= current;
                current = 0;
                f = -1;
            }
        }
        if (f != -1 && current) {
            ans++;
        }
        ans += cnt_0- current;
        cout << ans << endl;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>

using namespace std;

int main() {
	int count1{ 0 }, count2{ 0 }, a{ 0 }, b{ 0 }, n;
	string s, t;
	cin >> n;
	cin >> s >> t;
	for (int i = 0; i < n; ++i) {
		if (s[i] == '1') ++a;
		if (t[i] == '1') ++b;
		if (s[i] != t[i]) {
			if (s[i] == '1') {
				++count1;
				if (count2) --count2;
			}
			else if (s[i] == '0') {
				++count2;
				if (count1) --count1;
			}
		}
	}
	if (a == b) cout << (count1 + count2);
	else cout << "-1";
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>

using namespace std;

int main() {
	int count1{ 0 }, count2{ 0 }, n;
	string s, t;
	cin >> n;
	cin >> s >> t;
	for (int i = 0; i < n; ++i)
		if (s[i] != t[i]) {
			if (s[i] == '1') {
				++count1;
				if (count2) --count2;
			}
			else if (s[i] == '0') {
				++count2;
				if (count1) --count1;
			}
		}
	cout << (count1 + count2);
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mx 200007
using namespace std;

bool vis[mx];
int par[mx], Size[mx], Prev[mx],low[mx],d[mx];
vector <int> adj[mx], lim[2];
vector <pii> e, ans;
unordered_set<int>st;
int Time;
int s,t;

void init( int n )
{
    for(int i=0; i<=n; i++)
        Prev[i]=-1, par[i]= i, Size[i]= 1;
    Time=0;
}

void dfs(int u)
{
    vis[u]=1;
    low[u]=d[u]=(++Time);

    int subtree=0;
    int sz=adj[u].size();
    for(int i=0; i<sz; i++)
    {
        int v=adj[u][i];

        if(v==Prev[u])
            continue;

        if(!vis[v])
        {
            vis[v]=1;
            subtree++;
            Prev[v]=u;

            dfs(v);

            low[u]=min( low[u], low[v] );

            if(low[v]>d[u])
            {
                if( u==s )
                    lim[0].push_back(v);
                if( u==t )
                    lim[1].push_back(v);
                if( v==s )
                    lim[0].push_back(u);
                if( v==t )
                    lim[1].push_back(u);
            }

        }

        else
            low[u]=min( low[u], d[v] );
    }

}

int Find( int u )
{
    return (par[u]==u)?u:Find(par[u]);
}

void unite( int u, int v )
{
    int paru= Find( u );
    int parv= Find( v );

    if( paru!=parv )
    {
        if( Size[paru]>Size[parv] )
            par[parv]= paru, Size[paru]+= Size[parv];
        else
            par[paru]= parv, Size[parv]+= Size[paru];
    }
}

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

    init(n);

    for( int i=0; i<m; i++ )
    {
        int p,q;
        scanf("%d %d", &p, &q);

        e.push_back( pii(p,q) );

        adj[p].push_back(q);
        adj[q].push_back(p);
    }

    int x,y;

    scanf("%d %d %d %d", &s, &t, &x, &y);

    dfs(1);

    int xx= lim[0].size(), yy= lim[1].size();

    if( x<xx or y<yy )
        printf("No\n");
    else
    {
        for( int i=0; i<xx; i++ )
        {
            int p= lim[0][i];

            if( Find( s )!=Find( p ) )
            {
                ans.push_back(pii(s,p));
                st.insert(s);
                st.insert(p);
                unite( s, p );
            }
        }

        for( int i=0; i<yy; i++ )
        {
            int p= lim[1][i];

            if( Find( t )!=Find( p ) )
            {
                ans.push_back(pii(t,p));
                st.insert(t);
                st.insert(p);
                unite( t, p );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( (u==s and v==t) or (v==s and u==t) )
                continue;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }

                ans.push_back(pii(u,v));
                st.insert(u);
                st.insert(v);
                unite( u,v );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }
                ans.push_back(pii(u,v));
                st.insert(u);
                st.insert(v);
                unite( u,v );
            }
        }

        if( st.size()==n )
        {
            printf("Yes\n");

            for( int i=0;i<n-1;i++  )
                printf("%d %d\n", ans[i].f, ans[i].s);
        }
        else
            printf("No\n");

    }
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mx 200007
using namespace std;

bool vis[mx];
int par[mx], Size[mx], Prev[mx],low[mx],d[mx];
vector <int> adj[mx], lim[2];
vector <pii> e;
int Time;
int s,t;

void init( int n )
{
    for(int i=0; i<=n; i++)
        Prev[i]=-1, par[i]= i, Size[i]= 1;
    Time=0;
}

void dfs(int u)
{
    vis[u]=1;
    low[u]=d[u]=(++Time);

    int subtree=0;
    int sz=adj[u].size();
    for(int i=0; i<sz; i++)
    {
        int v=adj[u][i];

        if(v==Prev[u])
            continue;

        if(!vis[v])
        {
            vis[v]=1;
            subtree++;
            Prev[v]=u;

            dfs(v);

            low[u]=min( low[u], low[v] );

            if(low[v]>d[u])
            {
                if( u==s )
                    lim[0].push_back(v);
                if( u==t )
                    lim[1].push_back(v);
                if( v==s )
                    lim[0].push_back(u);
                if( v==t )
                    lim[1].push_back(u);
            }

        }

        else
            low[u]=min( low[u], d[v] );
    }

}

int Find( int u )
{
    return (par[u]==u)?u:Find(par[u]);
}

void unite( int u, int v )
{
    int paru= Find( u );
    int parv= Find( v );

    if( paru!=parv )
    {
        if( Size[paru]>Size[parv] )
            par[parv]= paru, Size[paru]+= Size[parv];
        else
            par[paru]= parv, Size[parv]+= Size[paru];
    }
}

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

    init(n);

    for( int i=0; i<m; i++ )
    {
        int p,q;
        scanf("%d %d", &p, &q);

        e.push_back( pii(p,q) );

        adj[p].push_back(q);
        adj[q].push_back(p);
    }

    int x,y;

    scanf("%d %d %d %d", &s, &t, &x, &y);

    dfs(1);

    int xx= lim[0].size(), yy= lim[1].size();

    if( x<xx or y<yy )
        printf("No\n");
    else
    {
        printf("Yes\n");

        for( int i=0; i<xx; i++ )
        {
            int p= lim[0][i];

            if( Find( s )!=Find( p ) )
            {
                printf("%d %d\n", s, p);
                unite( s, p );
            }
        }

        for( int i=0; i<yy; i++ )
        {
            int p= lim[1][i];

            if( Find( t )!=Find( p ) )
            {
                printf("%d %d\n", t, p);
                unite( t, p );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }
                printf("%d %d\n", u, v);
                unite( u,v );
            }
        }

    }
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mx 200007
using namespace std;

bool vis[mx];
int par[mx], Size[mx], Prev[mx],low[mx],d[mx];
vector <int> adj[mx], lim[2];
vector <pii> e;
int Time;
int s,t;

void init( int n )
{
    for(int i=0; i<=n; i++)
        Prev[i]=-1, par[i]= i, Size[i]= 1;
    Time=0;
}

void dfs(int u)
{
    vis[u]=1;
    low[u]=d[u]=(++Time);

    int subtree=0;
    int sz=adj[u].size();
    for(int i=0; i<sz; i++)
    {
        int v=adj[u][i];

        if(v==Prev[u])
            continue;

        if(!vis[v])
        {
            vis[v]=1;
            subtree++;
            Prev[v]=u;

            dfs(v);

            low[u]=min( low[u], low[v] );

            if(low[v]>d[u])
            {
                if( u==s )
                    lim[0].push_back(v);
                if( u==t )
                    lim[1].push_back(v);
                if( v==s )
                    lim[0].push_back(u);
                if( v==t )
                    lim[1].push_back(u);
            }

        }

        else
            low[u]=min( low[u], d[v] );
    }

}

int Find( int u )
{
    return (par[u]==u)?u:Find(par[u]);
}

void unite( int u, int v )
{
    int paru= Find( u );
    int parv= Find( v );

    if( paru!=parv )
    {
        if( Size[paru]>Size[parv] )
            par[parv]= paru, Size[paru]+= Size[parv];
        else
            par[paru]= parv, Size[parv]+= Size[paru];
    }
}

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

    init(n);

    for( int i=0; i<m; i++ )
    {
        int p,q;
        scanf("%d %d", &p, &q);

        e.push_back( pii(p,q) );

        adj[p].push_back(q);
        adj[q].push_back(p);
    }

    int x,y;

    scanf("%d %d %d %d", &s, &t, &x, &y);

    dfs(1);

    int xx= lim[0].size(), yy= lim[1].size();

    if( x<xx or y<yy )
        printf("No\n");
    else
    {
        printf("Yes\n");

        for( int i=0; i<xx; i++ )
        {
            int p= lim[0][i];

            if( Find( s )!=Find( p ) )
            {
                printf("%d %d\n", s, p);
                unite( s, p );
            }
        }

        for( int i=0; i<yy; i++ )
        {
            int p= lim[1][i];

            if( Find( t )!=Find( p ) )
            {
                printf("%d %d\n", t, p);
                unite( t, p );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( (u==s and v==t) or (v==s and u==t) )
                continue;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }

                printf("%d %d\n", u, v);
                unite( u,v );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }
                printf("%d %d\n", u, v);
                unite( u,v );
            }
        }

    }
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mx 200007
using namespace std;

bool vis[mx];
int par[mx], Size[mx], Prev[mx],low[mx],d[mx];
vector <int> adj[mx], lim[2];
vector <pii> e, ans;
unordered_set<int>st;
int Time;
int s,t;

void init( int n )
{
    for(int i=0; i<=n; i++)
        Prev[i]=-1, par[i]= i, Size[i]= 1;
    Time=0;
}

void dfs(int u)
{
    vis[u]=1;
    low[u]=d[u]=(++Time);

    int subtree=0;
    int sz=adj[u].size();
    for(int i=0; i<sz; i++)
    {
        int v=adj[u][i];

        if(v==Prev[u])
            continue;

        if(!vis[v])
        {
            vis[v]=1;
            subtree++;
            Prev[v]=u;

            dfs(v);

            low[u]=min( low[u], low[v] );

            if(low[v]>d[u])
            {
                if( u==s )
                    lim[0].push_back(v);
                if( u==t )
                    lim[1].push_back(v);
                if( v==s )
                    lim[0].push_back(u);
                if( v==t )
                    lim[1].push_back(u);
            }

        }

        else
            low[u]=min( low[u], d[v] );
    }

}

int Find( int u )
{
    return (par[u]==u)?u:Find(par[u]);
}

void unite( int u, int v )
{
    int paru= Find( u );
    int parv= Find( v );

    if( paru!=parv )
    {
        if( Size[paru]>Size[parv] )
            par[parv]= paru, Size[paru]+= Size[parv];
        else
            par[paru]= parv, Size[parv]+= Size[paru];
    }
}

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

    init(n);

    for( int i=0; i<m; i++ )
    {
        int p,q;
        scanf("%d %d", &p, &q);

        e.push_back( pii(p,q) );

        adj[p].push_back(q);
        adj[q].push_back(p);
    }

    int x,y;

    scanf("%d %d %d %d", &s, &t, &x, &y);

    dfs(1);

    int xx= lim[0].size(), yy= lim[1].size();

    if( x<xx or y<yy )
        printf("No\n");
    else
    {
        for( int i=0; i<xx; i++ )
        {
            int p= lim[0][i];

            if( Find( s )!=Find( p ) )
            {
                ans.push_back(pii(s,p));
                unite( s, p );
            }
        }

        for( int i=0; i<yy; i++ )
        {
            int p= lim[1][i];

            if( Find( t )!=Find( p ) )
            {
                ans.push_back(pii(t,p));
                unite( t, p );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( (u==s and v==t) or (v==s and u==t) )
                continue;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }

                ans.push_back(pii(u,v));
                unite( u,v );
            }
        }

        for( int i=0; i<m; i++ )
        {
            int u= e[i].f;
            int v= e[i].s;

            if( Find(u)!=Find(v) )
            {
                if( u==s or v==s )
                {
                    if( xx==x )
                        continue;
                    xx++;
                }
                if( u==t or v==t )
                {
                    if( yy==y )
                        continue;
                    yy++;
                }
                ans.push_back(pii(u,v));
                unite( u,v );
            }
        }

        if( Size[ Find(1) ]==n )
        {
            printf("Yes\n");

            for( int i=0;i<n-1;i++  )
                printf("%d %d\n", ans[i].f, ans[i].s);
        }
        else
            printf("No\n");

    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        int vals,valt;
        if(ds>=dt)vals=1,valt=2;
        else vals=2,valt=1;
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].y==s)a[i].w+=vals;
            if(a[i].x==t||a[i].y==t)a[i].w+=valt;
        }
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                    if(a[i].x==s||a[i].y==s)dds++;
                    if(a[i].y==t||a[i].y==t)ddt++;
                }
            }
        }
        if(dds<=ds&&ddt<=dt)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        int vals,valt;
        if(ds>=dt)vals=1,valt=2;
        else vals=2,valt=1;
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].y==s)a[i].w+=vals;
            if(a[i].x==t||a[i].y==t)a[i].w+=valt;
        }
        int cnt=0;
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    if(a[i].x==s||a[i].y==s)
                    {
                        if(dds==ds)continue;
                        dds++;
                    }
                    if(a[i].x==t||a[i].y==t)
                    {
                        if(ddt==dt)continue;
                        ddt++;
                    }
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
        }
        if(dds<=ds&&ddt<=dt&&cnt==n-1)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        int vals,valt;
        if(ds>=dt)vals=1,valt=2;
        else vals=2,valt=1;
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].y==s)a[i].w+=vals;
            if(a[i].y==t||a[i].y==t)a[i].w+=valt;
        }
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                    if(a[i].x==s||a[i].y==s)dds++;
                    if(a[i].y==t||a[i].y==t)ddt++;
                }
            }
        }
        if(dds<=ds&&ddt<=dt)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].x==t)a[i].w++;
            if(a[i].y==s||a[i].y==t)a[i].w++;
        }
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                    if(a[i].x==s||a[i].y==s)dds++;
                    if(a[i].y==t||a[i].y==t)ddt++;
                }
            }
        }
        if(dds<=ds&&ddt<=dt)
        {
            printf("Yes\n");
            for(int i=0;i<n;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        int vals,valt;
        if(ds>dt)vals=1,valt=2;
        else vals=2,valt=1;
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].y==s)a[i].w+=vals;
            if(a[i].x==t||a[i].y==t)a[i].w+=valt;
        }
        int cnt=0;
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    if(a[i].x==s||a[i].y==s)
                    {
                        if(dds==ds)continue;
                        dds++;
                    }
                    if(a[i].x==t||a[i].y==t)
                    {
                        if(ddt==dt)continue;
                        ddt++;
                    }
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
        }
        if(dds<=ds&&ddt<=dt&&cnt==n-1)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].x==t)a[i].w++;
            if(a[i].y==s||a[i].y==t)a[i].w++;
        }
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                    if(a[i].x==s||a[i].y==s)dds++;
                    if(a[i].y==t||a[i].y==t)ddt++;
                }
            }
        }
        if(dds<=ds&&ddt<=dt)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            printf("No\n");
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,use;
}a[550000];
int f[550000];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,ds,dt;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        int vals,valt;
        if(ds>dt)vals=1,valt=2;
        else vals=2,valt=1;
        for(int i=0;i<m;i++)
        {
            a[i].use=0;
            if(a[i].x==s||a[i].y==s)a[i].w=vals;
            if(a[i].x==t||a[i].y==t)a[i].w=valt;
        }
        int cnt=0;
        sort(a,a+m,cmp);
        int dds=0,ddt=0;
        for(int i=0;i<m;i++)
        {
            if(a[i].w==0)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
            else
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    if(a[i].x==s||a[i].y==s)
                    {
                        if(dds==ds)continue;
                        dds++;
                    }
                    if(a[i].x==t||a[i].y==t)
                    {
                        if(ddt==dt)continue;
                        ddt++;
                    }
                    cnt++;
                    a[i].use=1;
                    merge(a[i].x,a[i].y);
                }
            }
        }
        if(dds<=ds&&ddt<=dt&&cnt==n-1)
        {
            printf("Yes\n");
            for(int i=0;i<m;i++)
            {
                if(a[i].use==1)
                printf("%d %d\n",a[i].x,a[i].y);
            }
        }
        else
        {
            for(int i=1;i<=n;i++)f[i]=i;
            int vals,valt;
            if(ds>=dt)vals=1,valt=2;
            else vals=2,valt=1;
            for(int i=0;i<m;i++)
            {
                a[i].use=0;
                if(a[i].x==s||a[i].y==s)a[i].w=vals;
                if(a[i].x==t||a[i].y==t)a[i].w=valt;
            }
            int cnt=0;
            sort(a,a+m,cmp);
            int dds=0,ddt=0;
            for(int i=0;i<m;i++)
            {
                if(a[i].w==0)
                {
                    if(find(a[i].x)!=find(a[i].y))
                    {
                        cnt++;
                        a[i].use=1;
                        merge(a[i].x,a[i].y);
                    }
                }
                else
                {
                    if(find(a[i].x)!=find(a[i].y))
                    {
                        if(a[i].x==s||a[i].y==s)
                        {
                            if(dds==ds)continue;
                            dds++;
                        }
                        if(a[i].x==t||a[i].y==t)
                        {
                            if(ddt==dt)continue;
                            ddt++;
                        }
                        cnt++;
                        a[i].use=1;
                        merge(a[i].x,a[i].y);
                    }
                }
            }
            if(dds<=ds&&ddt<=dt&&cnt==n-1)
            {
                printf("Yes\n");
                for(int i=0;i<m;i++)
                {
                    if(a[i].use==1)
                    printf("%d %d\n",a[i].x,a[i].y);
                }
            }
            else
            {
                printf("No\n");
            }
        }
    }
}