//#undef DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll llinf = (1ll<<61)-1;
#define sz(a) int(a.size())
#define all(x) begin(x), end(x)
#ifdef DEBUG
const int DEBUG_END = 26;
#define DOS cout
#include <debug.h>
#else
#define bug(args...) void()
#define cbug(a, args...)
#endif
#define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b));
#define fi first
#define se second
int TC = 1, CN;
const int inf = 1000000007;
const int H = 1005;
int h, w, d[2];
string grid[H];
bitset<H> vstd[H], visd[2];
vector<pair<int, int>> g[2][H];

void dfs(pair<int, int> u) {
    auto [r, c] = u;
    int p[2] = {r, c};
    for (int i = 0; i < 2; i++) {
        if (visd[i][p[i]]) continue;
        visd[i][p[i]] = 1;
        for (auto &j : g[i][p[i]]) {
            if (!vstd[j.fi][j.se]) vstd[j.fi][j.se] = 1, dfs(j);
        }
    }
}

signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed);
    //cin >> TC;// inputAll + resetAll -> solution
    auto kase = [&]()->void {
        cin >> h >> w;
        d[0] = h, d[1] = w;
        for (int i = 0; i < h; i++) {
            cin >> grid[i];
            for (int j = 0; j < w; j++) {
                if (grid[i][j] == '#') g[0][i].push_back({i, j}), g[1][j].push_back({i, j});
            }
        }

        queue<pair<int, int> > q;
        for (int i = 0; i < 2; i++) {
            for (auto &j : g[i][0]) {
                if (!vstd[j.fi][j.se]) vstd[j.fi][j.se] = 1, dfs(j);
            }
            for (auto &j : g[i][d[i]-1]) {
                if (!vstd[j.fi][j.se]) vstd[j.fi][j.se] = 1, dfs(j);
            }
        }

        int cnt = 0, cntr = h-2, cntc = w-2;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (!vstd[i][j] and grid[i][j] == '#') ++cnt, dfs({i, j});
            }
        }
        for (int i = 1; i < h-1; i++) {
            bool ok = 0;
            for (int j = 0; j < w; j++) {
                ok |= vstd[i][j];
            }
            cntr -= ok;
        }
        for (int i = 1; i < w-1; i++) {
            bool ok = 0;
            for (int j = 0; j < h; j++) {
                ok |= vstd[j][i];
            }
            cntc -= ok;
        }
        cout << cnt+min(cntr, cntc) << '\n';
    };
    while (CN++!=TC) kase();
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,L;
    cin >> N >> L;
    vec<int> A(N+2),B(N+2);
    for(int i=1;i<=N;i++) cin >> A[i];
    for(int i=1;i<=N;i++) cin >> B[i];
    A[N+1] = B[N+1] = L+1;
    vec<int> C(N+1),D(N+1);
    for(int i=0;i<=N;i++){
        C[i] = A[i+1]-A[i]-1;
        D[i] = B[i+1]-B[i]-1;
    }
    ll ans = 0;
    int sum = 0,r = 0;
//    for(int i=0;i<=N;i++) cerr << C[i] << " " << D[i] << "\n";
    for(int i=0;i<=N;i++) if(D[i]){
        while(C[r]==0) r++;
        int l = r;
        while(r<=N && sum+C[r]<=D[i]){
            sum += C[r++];
            if(sum==D[i]) break;
        }
        if(sum!=D[i]){
            cout << "-1\n";
            return 0;
        }
//        cerr << l << " " << r << "\n";
        if(r<=i) ans += i-l;
        else if(i<=l) ans += r-i-1;
        else ans += r-l-1;
        sum = 0;
    }
    cout << ans << "\n";
}