#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
const int N = 500005;
char s[N], e[N];
vector<int> v[2];
vector<int> df;
int t[N];
int n;
int lowbit(int x){
	return x & -x;
}
void add(int x, int w){
	while(x <= n){
		t[x] += w;
		x += lowbit(x);
	}
}
int query(int x){
	int res = 0;
	while(x){
		res += t[x];
		x -= lowbit(x);
	}
	return res;
}
int main(){
	scanf("%d", &n);
	scanf("%s", s);
	scanf("%s", e);
	bool flag = 1;
	int ans = 0;
	for(int i = 1; i <= n; i++){
		if(s[i - 1] == '0') add(i, 1);
		if(s[i - 1] != e[i - 1]){
			v[s[i - 1] - '0'].push_back(i);
			df.push_back(i);
		}
	}
	df.push_back(n + 1);
	if(v[0].size() != v[1].size()) printf("-1\n");
	else{
		int l = df[0], mt = 0;
		for(int i = 0; i < df.size(); i++){
			if(s[df[i] - 1] == '0') mt--;
			else mt++;
//			cout << mt << endl;
			if(mt == 0){
				ans += query(df[i]) - query(l - 1);
				l = df[i + 1];
			}
		}
		printf("%d\n", ans);
	}
	return 0;
} 
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x)cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<pll, ll> &p1, const pair<pll, ll> &p2)const {
        if (p1.S == p2.S)
        {
            return p1.F.F > p2.F.F;
        }
        return p1.S < p2.S;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b, ll M = MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 2e5 + 5, inf = 2e18;

void solve()
{
    ll N, P;
    cin >> N >> P;
    ll ans = (P - 1);
    ans = ans * get_pow(P - 2, N - 1) % MOD;
    cout << ans << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test = 1;
    clock_t z = clock();
    //cin >> test;
    forn(tes, 0, test)
    {
        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}