#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define sqr(x) ((x) * (x))
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;

const int MOD = 1e9 + 7;
const LL MX = 3e3 + 10;
const LL INF = 1e9 + 10;

int main()
{
    //ofstream out("output.txt");
    //ifstream in("input.txt");
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    string s;
    cin>>s;
    deque<char> dq;
    int f = 0;

    for(char ch : s)
    {
        if(ch == 'R') f ^= 1;
        else if(f == 0)
        {
            if(dq.size() && dq.back() == ch) dq.pop_back();
            else dq.push_back(ch);
        }
        else
        {
            if(dq.size() && dq.front() == ch) dq.pop_front();
            else dq.push_front(ch);
        }
    }

    if(f == 0)
    {
        while(!dq.empty())
        {
            char ch = dq.front();
            dq.pop_front();
            cout<<ch;
        }
    }
    else
    {
        while(!dq.empty())
        {
            char ch = dq.back();
            dq.pop_back();
            cout<<ch;
        }
    }

    cout<<'\n';

    return 0;
}

#include "bits/stdc++.h"

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(x) cerr << #x << " == " << (x) << '\n';
#define all(X) begin(X), end(X)
#define size(X) (int)std::size(X)

using ll = long long;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

void fail()
{
    cout << -1 << endl;
    exit(0);
}

template<int n>
bitset<n> read_bitset()
{
    string str; cin >> str;
    reverse(all(str));
    return bitset<n>(str);
}

int main()
{ _
    constexpr int nmax = 5e5;

    int n; cin >> n;

    bitset<nmax> s = read_bitset<nmax>(), t = read_bitset<nmax>();

    for (int i = 1; i < n; ++i)
    {
        s[i] = s[i] ^ s[i - 1];
        t[i] = t[i] ^ t[i - 1];
    }

    ll ans = 0;

    for (int i = 0, j = 0; i < n; ++i, j = max(j, i))
    {
        if (s[j] == t[i]) continue;

        while (j < n && s[j] != t[i]) s[j++] = t[i];

        if (j == n) fail();

        ans += j - i;
    }

    cout << ans << endl;

    exit(0);
}
