//Awwawa! Dis cold yis ratten buy tEMMIE!
#include <bits/stdc++.h>
#define ll long long
#define maxn 200005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second

template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}

using namespace std;
ll ksm(ll a, ll b) {
   if (!b) return 1;
   ll ns = ksm(a, b >> 1);
   ns = ns * ns % mod;
   if (b & 1) ns = ns * a % mod;
   return ns;
}
int a[maxn], b[maxn];
map<int, int> s[2];
vi tot;
int ask(int l) {
	int pl = lower_bound(tot.begin(), tot.end(), l + 1) - tot.begin();
	return pl;
}
int ask(int l, int r) {
//	cout << l << ' ' << r << ' ' << ask(r) << ' ' << ask(l - 1) << endl;
	return ask(r) - ask(l - 1);
}
int main() {
	int n, l;
	cin >> n >> l;
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	a[0] = 0, a[n + 1] = l + 1;
	for (int i = 1; i <= n; i++)
		scanf("%d", &b[i]);
	b[0] = 0, b[n + 1] = l + 1;
	vi tr;
	for (int i = 0; i <= n + 1; i++)
		b[i] -= i, a[i] -= i, 
		s[0][a[i]]++, s[1][b[i]]++, 
		tr.pb(a[i]), tr.pb(b[i]);
	sort(tr.begin(), tr.end());
	tr.erase(unique(tr.begin(), tr.end()), tr.end());
	for (auto v : tr) {
		if (s[1][v] && !s[0][v]) {
			cout << -1 << endl;
			return 0;
		}
		if (s[0][v] && s[1][v])
			tot.pb(v);
	}
	ll ans = 0;
	for (int i = 0; i <= n + 1; i++) {
//		cout << a[i] << ' ' << b[i] << ' ' << ans << endl;
		if (a[i] > b[i])
			ans += ask(b[i], a[i] - 1);
		else ans += ask(a[i] + 1, b[i]);
	}

	cout << ans << endl;
	return 0;
}

/** これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました **/
#include<bits/stdc++.h>
using namespace std;

#ifdef Zoro
  #include "headers/debug.h"
#else
  #define debug(x...)
  #define deb(x...)
#endif

#define rep(i,x,y) for(int i=x;i<y;i++)
#define repr(i,x,y) for(int i=x;i>=y;i--)
#define int long long
#define pb push_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define all(x) begin(x), end(x)
#define memo(x,y) memset((x),(y),sizeof((x)))
#define line cout<<"[At "<<__LINE__<<"]"<<endl;

using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pair<int, int>>;

constexpr int mod = 1e9 + 7, N = 2e5 + 5;
constexpr long long inf = 1e18;
constexpr long double eps = 1e-6;


void Onigiri() {
  int n,m;
  cin>>n>>m;

  deque<pii> v(m);
  rep(i,0,m){
    cin>>v[i].ff>>v[i].ss;
  }
  sort(all(v));
  unordered_set<int> cols;
  cols.insert(n);
  while(sz(v)){
    int cx = v.front().ff;
    vi cy;
    while(sz(v) && v.front().ff == cx) {
      cy.pb(v.front().ss);
      v.pop_front();
    }

    vi addList,removeList;
    for(auto &x:cy){
      if(cols.find(x-1)!=cols.end()){
        addList.push_back(x);
      }else if(cols.find(x+1)!= cols.end()){
        addList.push_back(x);
      }else{
        removeList.push_back(x);
      }
    }

    deb(cx,addList,removeList);
    for(auto &x:removeList){
      if(cols.find(x)!=cols.end()) cols.erase(x);
    }

    for(auto &x:addList){
      if(cols.find(x)==cols.end()) cols.insert(x);
    }

  }

  cout<<sz(cols);
  
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //cout.precision(10);cout<<fixed;
#ifdef Zoro
  freopen("/home/pritish/Competitive/io/in", "r", stdin);
  freopen("/home/pritish/Competitive/io/out", "w", stdout);
#endif

  int t = 1;
  //cin >> t;

  while(t--) {
    Onigiri();
    cout << "\n";
  }

  cerr << "\n" << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}