#include <bits/stdc++.h>

#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)

const int mxn = 2e5 + 5;
int n, a[mxn], b[mxn];
std::map <int, std::vector <int> > oa, ob;

int s[mxn];

void update(int x, int v) {
	for (++ x; x < mxn; x += x & -x) s[x] += v;
}

int query(int x) {
	int ans = 0;
	for (++ x; x; x -= x & -x) ans += s[x];
	return ans;
}

int frm[mxn];

int main() {
	scanf("%d", &n);
	rep(i, n) scanf("%d", &a[i]), a[i] += i;
	rep(i, n) scanf("%d", &b[i]), b[i] += i;
	rep(i, n) oa[a[i]].push_back(i);
	rep(i, n) ob[b[i]].push_back(i);
	for (auto x : oa) {
		std::vector <int> va = x.second, vb = ob[x.first];
		if (va.size() != vb.size()) return printf("%d\n", -1), 0;
		rep(i, va.size()) frm[vb[i]] = va[i];
	}
	rep(i, n) update(i, 1);
	long long ans = 0;
	rep(i, n) ans += query(frm[i] - 1), update(frm[i], -1);
	printf("%lld\n", ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
void solve() {
    string s; cin >> s;
    deque<char> t;
    int n = s.size();
    int rev = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'R')rev = 1 - rev;
        else {
            if (rev == 0) {
                if (t.size() > 0 && t.back() == s[i]) t.pop_back();
                else t.push_back(s[i]);
            }
            else if (rev == 1) {
                if (t.size() > 0 && t.front() == s[i]) t.pop_front();
                else t.push_front(s[i]);
            }
        }
    }
    if (rev == 0) {
        for (int i = 0; i < (int)t.size(); ++i)cout << t[i];
        cout << endl;
    }
    else {
        for (int i = t.size() - 1; i >= 0; --i) {
            cout << t[i];
        }
        cout << endl;
    }
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; ++i)
    {
        solve();
    }
}