#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define io ios::sync_with_stdio(false); cin.tie(0)


const ll INF = 1L << 60;

struct edge { int to; ll cost; int k; };

struct graph{
    int V;           
    vector<vector<edge>> G;
    vector<ll> d;   

    graph(int n): V(n) { 
        G.resize(V);
        d.resize(V);
        rep(i, V) d[i] = INF;
    }

    void add_edge(int s, int t, ll cost, int k) {
        edge e1, e2;
        e1.to = t; e1.cost = cost; e1.k = k;
        e2.to = s; e2.cost = cost; e2.k = k;
        G[s].push_back(e1);
        G[t].push_back(e2);
    }

    void dijkstra(int s) {
        d[s] = 0;
        priority_queue<P, vector<P>, greater<P>> que; // min-heap
        que.push(P(0, s));  // d[i], i
        while (!que.empty()) { 
            P p = que.top(); que.pop();
            int v = p.second;
            if (d[v] < p.first) continue;
            for (auto e : G[v]) {
                if (d[v] % e.k == 0) {
                    if (d[e.to] > d[v] + e.cost) {
                        d[e.to] = d[v] + e.cost;
                        que.push(P(d[e.to], e.to));
                    }
                } else {
                    if (d[e.to] > d[v] + e.cost + e.k - (d[v] % e.k)) {
                        d[e.to] = d[v] + e.cost + e.k - (d[v] % e.k);
                        que.push(P(d[e.to], e.to));
                    }
                }
            }
        }
    }
};

int main()
{
    io;
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    graph G(n);
    rep(i, m) {
        int a, b, t, k;
        cin >> a >> b >> t >> k;
        G.add_edge(a-1, b-1, t, k);
    }
    G.dijkstra(x-1);
    if (G.d[y-1] == INF) cout << -1 << endl;
    else cout << G.d[y-1] << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;
const long long inf = 1LL<<60;
using ll = long long;

int main(){
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  auto solve = [&]() -> ll {
    deque<ll> a, b;
    for (int i=0; i<n; i++) {
      if (s[i] == '1') a.push_back(i);
      if (t[i] == '1') b.push_back(i);
    }
    if (a.size() < b.size() || (b.size() - a.size()) % 2 == 1) return -1;
    auto twopop = [&]() -> ll {
      ll res = a[1] - a[0];
      a.pop_front(), a.pop_front();
      return res;
    };
    ll res = 0;
    for (auto id : b) {
      while (!a.empty() && a[0] < id) {
        if (a.size() < 2) return -1;
        res += twopop();
      }
      if (a.empty()) return -1;
      res += a[0] - id;
      a.pop_front();
    }
    if (a.size() % 2 == 1) return -1;
    while (!a.empty()) res += twopop();
    return res;
  };
  cout << solve() << endl;
}
