#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL

typedef long long ll;
typedef pair<ll,ll> P;

ll key(vector<vector<P> > &dp, ll l, ll r) {
	if(l + 1 == r)return dp[l][r].FI;
	if(dp[l][r].SE != -1)return dp[l][r].FI;
	ll ret = dp[l][r].FI;
	for(ll i = l + 1;i < r;i++) {
		ret = max(ret, key(dp, l, i) + key(dp, i, r));
	}
	dp[l][r].SE = 0;
	return dp[l][r].FI = ret;
}

ll seica(vector<ll> &w, vector<P> &h) {
	ll n = w.size();
	vector<vector<P> > dp(n, vector<P>(n, MP(0, -1)));
	// cout << "in seica" << endl;
	// cout << "w: ";SHOW1d(w, n);
	REP(i, n) {
		ll now = w[i];
		for(ll j = i + 1;j < n;j++) {
			now += w[j];
			ll id = lower_bound(ALL(h), MP(now, 0LL)) - h.begin();
			if(id == 0)continue;
			// cout << i << " " << now << " " << id << " " << h[id].SE << endl;
			dp[i][j].FI = h[id-1].SE;
		}
	}
	ll ret = key(dp, 0, n-1);
	// cout << ret << endl;
	return ret;
}

int main(){
	cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

	ll n, m;cin >> n >> m;
	vector<ll> w(n);
	REP(i, n)cin >> w[i];
	sort(ALL(w));
	vector<P> h(m);
	ll v_min = LLINF;
	REP(i, m) {
		cin >> h[i].SE >> h[i].FI;
		v_min = min(v_min, h[i].FI);
	}
	h.PB(MP(LLINF, 0));

	sort(ALL(h));
	REP(i, m) {
		h[i + 1].SE = max(h[i].SE, h[i+1].SE);
	}
	// REP(i, m + 1)cerr << "sorted: " << h[i].FI << " " << h[i].SE << endl;

	if(*max_element(ALL(w)) > v_min) {
		cout << -1 << endl;
		return 0;
	}

	ll ans = LLINF;
	ll cou = 1;
	for(ll i = 1;i <= n;i++)cou *= i;
	REP(i, cou) {
		ans = min(ans, seica(w, h));
		next_permutation(ALL(w));
	}

	if(ans == LLINF)cout << -1 << endl;
	else cout << ans << endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V, int ME> class BIT {
public:
	V bit[1<<ME],val[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
	void set(int e,V v) { add(e,v-val[e]);}
	int lower_bound(V val) {
		V tv=0; int i,ent=0;
		for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
		return ent;
	}
};
BIT<int,20> TL,TR;;

int N,L;
int A[101010],B[101010];
int T[101010];
map<int,int> le,ri;
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>L;
	A[0]=B[0]=0;
	FOR(i,N) {
		cin>>A[i+1];
		A[i+1]-=i+1;
	}
	A[N+1]=B[N+1]=L-N;
	FOR(i,N) {
		cin>>B[i+1];
		B[i+1]-=i+1;
	}
	
	N+=2;
	
	FOR(i,N) if(ri.count(A[i])==0) ri[A[i]]=i;
	for(i=N-1;i>=0;i--) if(le.count(A[i])==0) le[A[i]]=i;
	
	
	FOR(i,N) {
		if(A[i]==B[i]) T[i]=i;
		if(A[i]<B[i]) {
			if(ri.count(B[i])==0) return _P("-1\n");
			T[i]=ri[B[i]];
			TR.set(T[i],1);
		}
		if(A[i]>B[i]) {
			if(le.count(B[i])==0) return _P("-1\n");
			T[i]=le[B[i]];
			TL.set(T[i],1);
		}
	}
	ll ret=0;
	FOR(i,N) {
		if(A[i]<B[i]) {
			ret++;
			ret+=TR(T[i]-1)-TR(i);
		}
		if(A[i]>B[i]) {
			ret++;
			ret+=TL(i-1)-TL(T[i]);
		}
	}
	
	
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
