#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define REV(i, n) for (int i = n - 1; i > 0; i--)
#define all(v) v.begin(), v.end()
#define PL pair<ll, ll>
#define PI pair<int,int>
#define len(s) (int)s.size()

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "/home/defineprogram/Desktop/Library/structure/UnionFind.cpp"

class UnionFind {
    int N;
    vector<int> par, siz;

   public:
    int find(int x) {
        assert(x < N);
        return (par[x] == x ? x : par[x] = find(par[x]));
    }
    void merge(int x, int y) {
        assert(x < N && y < N);
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (siz[x] > siz[y]) swap(x, y);
        par[x] = y;
        siz[y] += siz[x];
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        return siz[find(x)];
    }
    UnionFind(int N) : N(N), siz(N, 1), par(N) {
        iota(all(par), 0);
    }
};

/*
@brief Union Find
@docs docs/UnionFind.md
*/
#line 3 "main.cpp"

int H,W;
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin>>H>>W;
    UnionFind uf(H+W);
    uf.merge(0,H-1);
    uf.merge(0,H);
    uf.merge(0,H+W-1);
    rep(i,H)rep(j,W){
        char c; cin>>c;
        if(c=='#')uf.merge(i,j+H);
    }
    set<int>ans1,ans2;
    rep(i,H){
        ans1.insert(uf.find(i));
    }
    rep(i,W){
        ans2.insert(uf.find(i+H));
    }
    cout<<min(len(ans1),len(ans2))-1<<"\n";
}

#include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N;
ll A[301000];
ll B[301000];
struct SegmentTree {
private:
    int n;
    vector<ll> node;
 
public:
    SegmentTree() {
        int sz = 201000;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) node[i+n-1] = 0;
        for(int i=n-2; i>=0; i--) node[i] = (node[2*i+1] + node[2*i+2]);
    }
 
    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = (node[2*x+1] +node[2*x+2]);
        }
    }
    // hannkaikukann 
    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return node[k];
 
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return (vl + vr);
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    map<ll, vector<ll>> mpA, mpB;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        mpA[A[i]+i].push_back(i);
    }
    for(int i = 0; i < N; i++) {
        cin >> B[i];
        mpB[B[i]+i].push_back(i);
    }
    vector<ll> p(N);
    for(auto tmp : mpA) {
        auto v = tmp.second;
        auto u = mpB[tmp.first];
        if(v.size() != u.size()) {
            cout << "-1" << endl;
            return 0;
        }
        for(int i = 0; i < u.size(); i++) {
            p[u[i]] = v[i];
        }
    }
    ll ans = 0;
    SegmentTree seg;
    for(auto val : p) {
        ans += seg.getsum(val, N);
        seg.update(val, 1);
    }
    cout << ans << endl;
    return 0;
}
