#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class T> inline T popFirst(multiset<T> &a){
  T res = *(a.begin());
  a.erase(a.begin());
  return res;
}
template<class T> inline T getFirst(multiset<T> &a){
  return *(a.begin());
}
template<class T> inline T popFirst(set<T> &a){
  T res = *(a.begin());
  a.erase(a.begin());
  return res;
}
template<class T> inline T getFirst(set<T> &a){
  return *(a.begin());
}
int N;
char S[500000+2];
char T[500000+2];
int main(){
  int i;
  int x;
  int y;
  long long res = 0;
  set<int> ss;
  set<int> tt;
  rd(N);
  rd(S);
  rd(T);
  for(i=(0);i<(N);i++){
    if(S[i]=='1'){
      ss.insert(i);
    }
  }
  for(i=(0);i<(N);i++){
    if(T[i]=='1'){
      tt.insert(i);
    }
  }
  while(ss.size() || tt.size()){
    if(ss.size()==0){
      wt_L(-1);
      wt_L('\n');
      return 0;
    }
    if(tt.size()==0 || getFirst(ss) < getFirst(tt)){
      if(ss.size()==1){
        wt_L(-1);
        wt_L('\n');
        return 0;
      }
      x = popFirst(ss);
      y = popFirst(ss);
      res += y - x;
      continue;
    }
    else{
      x = popFirst(ss);
      y = popFirst(tt);
      res += x - y;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20201115-1

// --- original code ---
// int N;
// char S[5d5+2], T[5d5+2];
// {
//   int x, y;
//   ll res = 0;
//   set<int> ss, tt;
//   rd(N,S,T);
//   rep(i,N) if(S[i]=='1') ss.insert(i);
//   rep(i,N) if(T[i]=='1') tt.insert(i);
//   while(ss.size() || tt.size()){
//     if(ss.size()==0) wt(-1), return 0;
//     if(tt.size()==0 || getFirst(ss) < getFirst(tt)){
//       if(ss.size()==1) wt(-1), return 0;
//       x = popFirst(ss);
//       y = popFirst(ss);
//       res += y - x;
//       continue;
//     } else {
//       x = popFirst(ss);
//       y = popFirst(tt);
//       res += x - y;
//     }
//   }
//   wt(res);
// }

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

int main()
{
  FAST/**/
   
   ll n,m;
   cin>>n>>m;
   
   string arr[n];
   ll cnt = 0;
   rep(n)
        cin>>arr[i];
   rep(n-1)
   {
       repi(j,m)
            if(arr[i][j] == '.' && arr[i+1][j] == '.')
                cnt++;
   }
   
   repi(j,m-1)
        rep(n)
            if(arr[i][j] == '.' && arr[i][j+1] == '.')
                cnt++;
    
   cout<<cnt;             
   
   
  return 0;   
  
}  

  
          
  
          
   