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

// for loop
#define rep(i,a,n) for(int i=(a);i<(n);++i)
#define repe(i,a,n) for(int i=(a);i<=(n);++i)
#define rrep(i,n,a) for(int i=(n);n>(a);--i)
#define rrepe(i,n,a) for(int i=(n);n>=(a);--i)

// abbreviati
#define PB(v) push_back(v)
#define MP(A,B) make_pair(A,B)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define ENDL << "\n"

using ll = long long;
using P = pair<int,int>;
using itn = int;

// constant
const int INF = 1<<30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007;
const long double PI =(acos(-1));

/***Snippet***************************************************
ifelse, isprime, torad, todeg, lcm, bfs,chmin,chmax
**************************************************************/

// adjacency list
vector<int> to[100005];

// Graph
using Graph = vector<vector<int>>;

// Grid
int H,W;
vector<vector<int>> field;

// 4方向への移動ベクトル
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h,w; cin >> h >> w;
    vector<string> s(h);
    rep(i,0,h){
        cin >> s[i];
    }

    bool flag=0;
    int cnt=0;
    rep(i,0,h){
        rep(j,0,w){
            flag=0;
            //if(j==w-1 || i==h-1) continue;
            if(s[i][j]=='.'){
                flag=1;
            }
            if(j<w-1 && flag && s[i][j+1]=='.'){
                cnt++;
            }
            if(i<h-1 && flag && s[i+1][j]=='.'){
                cnt++;
            }
        }
    }

    cout << cnt ENDL;
    
    return 0;
}
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define N 100012
inline int sgn(int x){return (x==0)?0:((x>0)?1:(-1));}
inline int Abs(int x){return (x>0)?x:(-x);}
int n,L,a[N],b[N],c[N],la[N];bool fl=true;map<int,int>mp;long long ans=0;
inline void Fin(int l,int r)
{
	if(l+1>=r)return;int i,ql=l,qr=r,ls;
	for(i=l+1;i<r-1;i++)if((c[i]==1)&&(c[i+1]==(-1))){fl=false;return;}
	for(i=l;i<=r;i++)if(c[i]==(-1))ql=i;for(i=r;i>=l;i--)if(c[i]==1)qr=i;
	mp.clear();for(i=l;i<=ql;i++){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;}
	mp.clear();for(i=r;i>=qr;i--){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;}
	ls=l;for(i=l;i<=r;i++){if(c[i]&&(ls>la[i])){fl=false;return;}if(c[i])ls=max(ls,la[i]);}
	ls=r;for(i=r;i>=l;i--){if(c[i]&&(ls<la[i])){fl=false;return;}if(c[i])ls=min(ls,la[i]);}
	ls=r;for(i=ql;i>=l;i--)if(c[i]&&(la[i]!=ls)){ans+=i-la[i];ls=la[i];}
	ls=l;for(i=qr;i<=r;i++)if(c[i]&&(la[i]!=ls)){ans+=la[i]-i;ls=la[i];}
}
int main(){
	scanf("%d%d",&n,&L);int i,l,r;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)scanf("%d",&b[i]);a[0]=b[0]=0;a[n+1]=b[n+1]=L+1;
	for(i=1;i<=n;i++)c[i]=sgn(b[i]-a[i]);
	for(i=0;(i<=(n+1))&&fl;i=r){l=i;r=i+1;while((r<=n)&&c[r])++r;Fin(l,r);}if(!fl){printf("-1");return 0;}printf("%lld",ans);return 0;
}