#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int a[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, cnt = 0;
  cin >> n;
  string S, T, s, t;
  cin >> S >> T;
  for (int i = 0; i < n; i++) {
    if (S[i] != T[i]) s += S[i], t += T[i];
    if (S[i] == '0') cnt++;
    if (T[i] == '0') cnt--;
  }
  if (cnt != 0) return cout << -1 << "\n", 0;
  n = s.size();
  int m = 1, block = 1;
  char cur = s[0];
  for (int i = 1; i < n; i++) {
    if (s[i] != cur)
      a[m++] = block, block = 1, cur = s[i];
    else
      block++;
  }
  a[m] = block;
  if (s[0] == s[n - 1] && block != n) a[1] += a[m], m--;
  int mn = 0, mx = 0, MN = 0, MX = 0, sum = 0;
  for (int i = 1; i <= m; i++) {
    sum += a[i] * (i % 2 ? 1 : -1);
    MX = max(MX, sum - mn), MN = min(MN, sum - mx);
    mx = max(mx, sum), mn = min(mn, sum);
  }
  int ans = max(max(MX, -MN), max(sum - MN, MX - sum));
  cout << ans << "\n";
  return 0;
}
