#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; auto range(int n) { return views::iota(0, n); } #define repeat(n) for ([[maybe_unused]] auto : range(n)) template ostream& operator<<(ostream& os, const pair& p){ return os << "{" << p.first << ", " << p.second << "}"; } template ostream& operator<<(ostream& os, const vector& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const set& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const map& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif struct UFWithWeights { vector data, weights; UFWithWeights(int n) : data(n, -1), weights(n, 1) {} int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } bool find(int x, int y) { return root(x) == root(y); } void uni(int x, int y) { x = root(x); y = root(y); if (x != y) { int new_weight = weights[x] + weights[y]; if (data[x] < data[y]) swap(x, y); data[x] += data[y]; data[y] = x; weights[x] = new_weight; } } int weight(int x) { return weights[root(x)]; } }; int get(int x, int i) { return (x >> i) & 1; } void experiment() { const int n = 11; UFWithWeights uf(1 << n); for (int x : range(1 << n)) { for (int i : range(n - 2)) { if (get(x, i) == get(x, i + 2)) { int y = x ^ (1 << (i + 1)); uf.uni(x, y); } } } vector> vss(1 << n); for (int x : range(1 << n)) vss[uf.root(x)].push_back(x); for (auto vs : vss) if (vs.size() > 0) { int m = *min(vs.begin(), vs.end()); // cout << vs << endl; auto s = bitset(m).to_string(); if (s[0] == '0' && s[n - 1] == '0') { for (auto x : vs) cout << bitset(x).to_string() << " "; cout << endl; } } } ll solve() { int n; cin >> n; string s_in, t_in; cin >> s_in >> t_in; if (s_in[0] != t_in[0]) return -1; if (s_in[n - 1] != t_in[n - 1]) return -1; vector s(n - 1), t(n - 1); for (int i : range(n - 1)) { s[i] = (s_in[i] - 'A') ^ (s_in[i + 1] - 'A'); t[i] = (t_in[i] - 'A') ^ (t_in[i + 1] - 'A'); } dump(s); dump(t); int i = 0, j = 0; ll res = 0; while (i < n - 1 && j < n - 1) { // In [0, i), s and t are the same // In [i, j), s and t are not the same and s is alternating if (s[i] == t[i]) { i++; continue; } assert(s[i] != t[i]); if (j < i) j = i; if (j - 1 >= i && s[j] == s[j - 1]) { // i j // 01010100 // -> // 11010101 res += j - i; s[i] ^= 1; s[j] ^= 1; } else { j++; } } if (i == n - 1) return res; else return -1; } int main() { cout << fixed << setprecision(12); // experiment(); cout << solve() << endl; }