#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct UnionFind {
  vector<ll> par, s; 
  UnionFind(ll N) : par(N), s(N) { rep(i,N) { par[i] = i; s[i] = 1; } }
  ll root(ll x) { return par[x]==x ? x : par[x] = root(par[x]); }
  ll size(ll x) { return par[x]==x ? s[x] : s[x] = size(root(x)); }
  void unite(ll x, ll y) { ll rx=root(x), ry=root(y); if (rx!=ry) { s[rx] += s[ry]; par[ry] = rx; } }
  bool same(ll x, ll y) {  ll rx=root(x), ry=root(y); return rx==ry; }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll H, W; cin >> H >> W;
  string S[H]; rep(i,H) cin >> S[i];

  UnionFind uf(H+W);
  rep(i,H) rep(j,W) {
    if ((i==0||i==H-1)&&(j==0||j==W-1)) uf.unite(i,j+H);
    if (S[i][j]=='#') uf.unite(i,j+H);
  }
  set<ll> sh, sw;
  rep(i,H) sh.insert(uf.root(i));
  rep(i,W) sw.insert(uf.root(i+H));

  // for (auto x: sh) cout << x << " "; cout << endl;
  // for (auto x: sw) cout << x << " "; cout << endl;
  ll result = min(sh.size(),sw.size()) - 1;
  cout << result << endl;
  return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <iomanip>
#include <utility>
typedef long long ll;
typedef long double ld;
using namespace std;

int A[100010], B[100010];

int main() {
	int N, L;
	cin >> N >> L;
	A[0]=B[0]=0, A[N+1]=B[N+1]=L+1;
	for(int i=1; i<=N; ++i) cin >> A[i];
	for(int i=1; i<=N; ++i) cin >> B[i];
	vector<int> v;
	for(int i=0; i<=N+1; ++i){
		if(A[i]==B[i]) v.emplace_back(i);
	}
	ll ans=0;
	for(int t=0; t+1<v.size(); ++t){
		int i=v[t]+1;
		while(A[i]>B[i]) ++i;
		int j=i, k=i+1;
		while(j<v[t+1]){
			while(k<=v[t+1]){
				if(A[k]-B[j]==k-j) break;
				++k;
			}
			if(k>v[t+1]){
				cout << -1 << endl;
				return 0;
			}
			ans += k-j;
			int m=j+1;
			while(m<v[t+1]){
				if(B[m]-B[j]>m-j) break;
				++m;
			}
			j=m;
		}
		j=i-1, k=i-2;
		while(j>v[t]){
			while(k>=v[t]){
				if(B[j]-A[k]==j-k) break;
				--k;
			}
			if(k<v[t]){
				cout << -1 << endl;
				return 0;
			}
			ans += j-k;
			int m=j-1;
			while(m>v[t]){
				if(B[j]-B[m]>j-m) break;
				--m;
			}
			j=m;
		}
	}
	cout << ans << endl;
	return 0;
}