/* これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました */

#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x,y) memset(x,y,sizeof x)

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;

const int mod = 1e9+7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;

void solve()
{
	int n;
	cin>>n;
	string s,t;
	cin>>s;
	cin>>t;
	if(count(all(s),'1') != count(all(t),'1'))
	{
		cout<<-1<<'\n';
		return;
	}
	ll ans=0;
	
	int cnt1=0,cnt2=0;
	
	for(int i=0;i<n;i++)
	{
		if(s[i]=='0') cnt1++;
		if(t[i]=='0') cnt2++;
		if(cnt1>0 && cnt2>0)
		{
			if(cnt1==1 && cnt2==1)
			{
				cnt1--;
				cnt2--;
				if(s[i]!='0' || t[i]!='0') ans++;
			}
			else if(cnt1>cnt2)
			{
				ans += cnt2;
				cnt1 -= cnt2;
				cnt2 = 0;
			}
			else
			{
				assert(cnt2>cnt1);
				ans += 1LL*cnt1*(cnt1+1)/2;
				cnt2 -= cnt1;
				cnt1 = 0;
			}
		}
	}
	
	cout<<ans<<'\n';
	
}

int main()
{
	IOS
	int t=1;
	//cin>>t;
	while(t--)
	{
		solve();
	}
}

#include <bits/stdc++.h>
using namespace std;

#ifndef aa
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
//const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;

//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = "; _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END




const int N = 5e5+5;


void solve(){
	
	int n, m;
	cin>>n>>m;
	vi a(n), b(m), c(n), d(n);
	rep(i,0,n)cin>>a[i];
	rep(i,0,m)cin>>b[i];
	
	sort(all(a));
	int ans = infl;
	
	for(int i=1; i<n; i+=2){
		c[i] = (i>1 ? c[i-2] : 0) + a[i] - a[i-1];
	}
	
	for(int i=n-2; i>=0; i-=2){
		d[i] = (i<n-2 ? d[i+2] : 0) + a[i+1] - a[i];
	}
	
	trav(t, b){
		int i = lb(all(a),t) - a.begin();
		if(i&1)
			i--;
		int x = abs(a[i] - t);
		if(i!=n-1)
			x += d[i+1];
		if(i)
			x += c[i-1];
		mini(ans, x);
	}
	
	cout << ans;
}



signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout << fixed << setprecision(15);
    int t = 1;
   // cin >> t;
    while (t--)
        solve();
#ifdef aa
    cout << endl << endl << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
    return 0;
}

int powm(int a, int b){
    int res = 1;
    while (b) {
        if (b & 1)
            res = (res*a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}
 
int divide(int a, int b) {
    return (a % mod) * powm(b, mod - 2) % mod;
}
 
int norm(int a) {
    while (a >= mod) a -= mod;
    while (a < 0) a += mod;
    return a;
}