#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

#define MAX_N 200005

class UnionFind
{
public:
  int par[MAX_N];
  int depth[MAX_N];
  int nGroup[MAX_N];

  UnionFind(int n) {
    init(n);
  }

  void init(int n) {
    for(int i=0; i<n; i++) {
      par[i] = i;
      depth[i] = 0;
      nGroup[i] = 1;
    }
  }

  int root(int x) {
    if(par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

  bool same(int x, int y) {
    return root(x) == root(y);
  }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return;

    if(depth[x] < depth[y]) {
      par[x] = y;
      nGroup[y] += nGroup[x];
      nGroup[x] = 0;
    } else {
      par[y] = x;
      nGroup[x] += nGroup[y];
      nGroup[y] = 0;
      if(depth[x] == depth[y])
        depth[x]++;
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<ll> A(n); rep(i, n) cin >> A[i];
  UnionFind uf(200001);
  rep(i, n) {
    uf.unite(A[i], A[n-1-i]);
  }
  ll ans = 0;
  rep(i, 200001) {
    if(uf.root(i) == i) {
      ans += uf.nGroup[i]-1;
    }
  }
  cout << ans << endk;
  return 0;
}

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

const int N=110000;
int n,L,a[N],b[N],c[N],d[N];
long long ans;
int main() {
	cin>>n>>L;
	for(int i=1;i<=n;++i) cin>>a[i], c[i]=a[i]-a[i-1]-1; c[n+1]=L-a[n];
	for(int i=1;i<=n;++i) cin>>b[i], d[i]=b[i]-b[i-1]-1; d[n+1]=L-b[n];
	for(int i=1,j=1;i<=n+1;++i) if(d[i]) {
		int sum=0,las=j;
		while(c[las] == 0) ++las;
		j=las;
		while(sum<d[i]) sum += c[j], ++j;
		if(sum != d[i]) puts("-1"), exit(0);
		ans += max(i-las,0)+max(j-i-1,0);
	}
	cout<<ans;
	return 0;
}