#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
template <typename T>
struct fenwick {
    int n, lgn;
    vector<T> fenw;
    fenwick(int n): n(n) {
        for(lgn = 0; (1<<lgn) <= n; lgn++);
        fenw.resize(n);
    }
    T get(int p){
        T r = T();
        for(; p; p -= p & -p)
            r = r + fenw[p];
        return r;
    }
    T get(int l, int r){
        return get(r) - get(l - 1);
    }
    void add(int p, T v){
        for(; p < n; p += p & -p)
            fenw[p] = fenw[p] + v;
    }
    int lower_bound(T v){
        int p = 0;
        T sum = 0;
        for(int i = lgn - 1; i >= 0; i--){
            int np = p + (1<<i);
            if(np < n && sum + fenw[np] < v){
                p = np;
                sum = sum + fenw[p];
            }
        }
        return p + 1;
    }
};
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for(int i = 0; i < n; i++) cin >> a[i];
	for(int i = 0; i < n; i++) cin >> b[i];
	using ll = long long;
	ll d = 0;
	for(int i = 0; i < n; i++) d += a[i] - b[i];
	if(d != 0) return cout << -1 << '\n', 0;
	for(int i = 0; i < n; i++) a[i] += i, b[i] += i;
	map<int, vector<int>> pos;
	for(int i = n - 1; i >= 0; i--){
		pos[b[i]].push_back(i);
	}
	for(int i = 0; i < n; i++){
		if(pos[a[i]].empty()){
			return cout << -1 << '\n', 0;
		}
		int x = pos[a[i]].back();
		pos[a[i]].pop_back();
		a[i] = x + 1;
	}
	fenwick<int> fenw(n + 1);
	long long ans = 0;
	for(int i = 0; i < n; i++){
		ans += i - fenw.get(a[i]);
		fenw.add(a[i], 1);
	}
	cout << ans << '\n';
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

int n;
long long t, arr[45];
vector<int> v[2];
vector<long long> p[2];
int id;
long long ans = 0;

void solve(int idx, long long sum) {
    if (sum > t) return;

    if (idx == v[id].size()) {
        // cout << "SUM " << sum << endl;
        p[id].push_back(sum);
        return;
    }
    solve(idx + 1, sum);
    solve(idx + 1, sum + v[id][idx]);
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);

    cin >> n >> t;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    sort(arr + 1, arr + n + 1);
    reverse(arr + 1, arr + n + 1);

    for (int i = 1; i <= n / 2; i++) v[0].push_back(arr[i]);
    for (int i = n / 2 + 1; i <= n; i++) v[1].push_back(arr[i]);
    id = 0;
    solve(0, 0);
    id = 1;
    solve(0, 0);
    for (int i = 0; i < 2; i++) sort(p[i].begin(), p[i].end());

    // for (long long x : p[0]) cout << "0 x " << x << "\n";
    // for (long long x : p[1]) cout << "1 x " << x << "\n";
    for (long long x : p[0]) {

        int pos = (int)(lower_bound(p[1].begin(), p[1].end(), t + 1 - x) - p[1].begin());
        if (pos == 0) continue;
        if (p[1][pos - 1] + x > t) continue;
        ans = max(ans, x + p[1][pos - 1]);
    }
    cout << ans << "\n";


    return 0;
}
