#include <bits/stdc++.h>
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
using namespace std;
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; }
typedef long long ll;
using P = pair<ll, int>;
const ll INF = 1e18;

struct Edge {
    int to, t, k;
    Edge(int to, int t, int k): to(to), t(t), k(k) {}
};

int main() {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    --x; --y;
    
    vector<vector<Edge>> g(n);
    REP(i, m){
        int a, b, t, k;
        cin >> a >> b >> t >> k;
        --a; --b;
        g[a].emplace_back(b, t, k);
        g[b].emplace_back(a, t, k);
    }

    vector<ll> dist(n, INF);
    priority_queue<P, vector<P>, greater<P>> pq;

    auto push = [&](int v, ll x){
        if(dist[v] <= x) return;
        dist[v] = x;
        pq.emplace(x, v);
    };

    push(x, 0);

    while(!pq.empty()){
        auto [x, v] = pq.top(); pq.pop();
        if(dist[v] != x) continue;
        for(Edge& e : g[v]) {
            ll nx = (x + e.k - 1)/e.k*e.k + e.t;
            push(e.to, nx);
        }
    }

    ll ans = dist[y];
    if(ans == INF) ans = -1;
    cout << ans << endl;

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
typedef long long ll;

int n,s[N],t[N],from[N];
char str[N];
set<int> pos[2];
ll ans;

int main() {
#ifdef LOCAL
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>(str+1);
	for(int i=1;i<=n;i++) s[i]=str[i]-'0';
	cin>>(str+1);
	for(int i=1;i<=n;i++) t[i]=str[i]-'0';
	for(int i=1;i<=n;i++) s[i]^=s[i-1],t[i]^=t[i-1],pos[s[i]].insert(i);
	for(int i=1;i<=n;i++) {
		auto it=pos[t[i]].lower_bound(max(from[i-1],i));
		if(it==pos[t[i]].end()) return cout<<-1,0;
		from[i]=*it;
	}
	for(int i=1;i<=n;i++)
		if(from[i]!=from[i-1]) ans+=from[i]-i;
	cout<<ans;
	return 0;
}
