#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vc vector<char>
#define vs vector<string>
#define vd vector<double>
#define vll vector<ll>
#define vvi vector<vi>
#define vvii vector<vii>
#define vvc vector<vc>
#define vvs vector<vs>
#define vvll vector<vll>
#define vvd vector<vd>
#define FOR(x,n) for(int x=0;x<n;x++)
#define FORS(x,n) for(int x=1;x<=n;x++)
#define FORE(x,a) for(auto &x: a)
#define ALL(x) x.begin(),x.end()
#define REP(n) for(int _ = 0; _ < n; _++)
#define MT make_tuple
#define pb push_back
#define endl '\n'
#define F first
#define S second

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


typedef tree<ii,
null_type,
less<ii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int h,w,m;
  cin >> h >> w >> m;
  vi mw(w,h),mh(h,w);
  FOR(i,m) {
    int x,y;
    cin >> x >> y;
    x--;y--;
    mh[x] = min(y,mh[x]);
    mw[y] = min(x,mw[y]);
  }
  ll sm = 0;
  FOR(i,mh[0]) {
    sm += mw[i];
  }
  FOR(i,mw[0]) {
    sm += mh[i];
  }


  //cout << " ! " << sm << endl;
  // subtract:

  ordered_set s;
  vii t;

  FOR(i,mw[0]) {
    t.pb({mh[i],i});
  }
  sort(ALL(t));
  int hp = 0;
  FOR(i,mw[0]) {
    while(t[i].F > hp && mh[0] > hp){
      s.insert({mw[hp],hp});
      hp++;
    }
    int lt = s.order_of_key({t[i].S,1e9});
    int gt = s.size() - lt;
    sm -= gt;
    //cout << " " << gt << " " << lt  << " " << t[i].F << " " << t[i].S << endl;
  }

  cout << sm << endl;
}

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

#define M1 1000000007
#define M2 998244353
#define INF 1e18
#define ll long long
#define pll pair<ll,ll>
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPR(i,a,b) for(ll i=b-1;i>=a;i--)
#define forr(i,n) for(ll i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
#define DB pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<int>
#define vlli vector <long long>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)

/********************************************************************/
const double pi = 3.1415926535;
/********************************************************************/
//FAST IO//
void FAST() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

/********************************************************************/
int main()
{
  FAST();
  int n; cin>>n;
  string s,t; cin>>s>>t;
  int ans=0, ct1=0, ct2=0;
  for (int i=0; i<n; i++)
  {
    if (s[i]=='0' && t[i]=='0')
    {
      if (ct1==ct2){continue; }
      ct1++; ct2++; ans++;
    }
    else if (s[i]=='0')
    {
      ct1++; ans++;
    }
    else if (t[i]=='0')
    {
      ct2++;
    }
  }
  if (ct1!=ct2){cout<<-1<<endl; return 0;}
  else {cout<<ans<<endl ;}
}