#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk make_pair
#define pb push_back
#define eb emplace_back
#define ins insert
#define fi first
#define se second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=5e5+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
//inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc(){return getchar();}
inline int read(){
	int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
	x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}
ll ans=0;
int n,pos1[MAXN],pos2[MAXN],cnt1,cnt2;char s[MAXN],t[MAXN];
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	n=read(),scanf("%s%s",s+1,t+1);
	Rep(i,1,n)if(s[i]=='1')pos1[++cnt1]=i;
	Rep(i,1,n)if(t[i]=='1')pos2[++cnt2]=i;
	int j=1;
	Rep(i,1,cnt2){
		while(pos1[j]<pos2[i]&&j<=cnt1)ans+=pos1[j+1]-pos1[j],j+=2;
		if(j>cnt1)return puts("-1"),0;ans+=pos1[j]-pos2[i],j++;
	}if((cnt1-j+1)%2)return puts("-1"),0;
	for(;j<=cnt1;ans+=pos1[j+1]-pos1[j],j+=2);
	cout<<ans<<"\n";
	return 0;
}


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

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 20;

int G[N][N];
int n;
int col[N];

int bit(int mask, int x) {
  return (mask >> x) & 1;
}

bool singlecol(int mask) {
  rep(i,0,n) if(bit(mask, i)) {
    rep(j,i+1,n) if(bit(mask, j)) {
      if(G[i][j]) return false;
    }
  }
  return true;
}

bool dfs(int c, int mask, int cc) {
  if(col[c] != -1) {
    return col[c] == cc;
  }
  col[c] = cc;
  bool res = true;
  rep(i,0,n) if(bit(mask, i) && G[c][i]) {
    res &= dfs(i, mask, cc ^ 1);
  }
  return res;
}

LL twocol(int mask) {
  memset(col, -1, sizeof(col));
  LL res = 1;
  rep(i,0,n) if(bit(mask, i) && col[i] == -1) {
    if(!dfs(i, mask, 0)) {
      res = 0;
    } else {
      res *= 2;
    }
  }
  return res;
}

int main() {
  int m;
  S2(n,m);
  rep(i,0,m) {
    int x,y;
    S2(x,y);
    x--; y--;
    G[x][y] = G[y][x] = 1;
  }
  int mx = (1 << n) - 1;
  LL ans = 0;
  rep(i,0,1<<n) {
    if(!(i & 1)) continue;
    if(singlecol(i)) {
      ans += twocol((~i) & mx);
      // printf("%d %lld\n",i,ans);
    }
  }
  ans *= 3;
  cout << ans << "\n";
  return 0;
}
