#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;

int n;
int g[21];
bool memo2[1<<18];
bool visited[1<<18];
bool isOK(int pat) {
  bool &res = memo2[pat];
  if(!visited[pat]) {
    visited[pat] = true;
    res = true;
    REP(i, n) {
      if(pat & (1 << i)) {
        if((g[i] & pat) != pat) {
          res = false;
          break;
        }
      }
    }
  }
  return res;
}
  
int memo[1 << 18];

int solve(int rest) {
  int &res = memo[rest];
  if(res < 0) {
    if(rest == 0) {
      res = 0;
    } else if(((rest-1) & rest) == 0) {
      res = 1;
    } else {
      int first = rest & -rest;
      int rest2 = rest ^ first;
      res = 1 + solve(rest2);
      for(int s = rest2; s; s = (s-1) & rest2) {
        if(isOK(s | first)) {
          res = min(res, 1 + solve(rest ^ first ^ s));
        }
      }
    }
  }
  return res;
}

int main(void) {
  int nEdge;
  scanf("%d%d", &n, &nEdge);
  REP(i, n) {
    g[i] |= 1 << i;
  }
  REP(i, nEdge) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a] |= 1 << b;
    g[b] |= 1 << a;
  }
  // if(nEdge == n*(n-1) / 2) {
  //   cout << 1 << endl;
  //   return 0;
  // }
  memset(memo, -1, sizeof memo);
  int res = solve((1 << n)-1);
  cout << res << endl;
  
  return 0;
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>

using namespace std;

clock_t timeStart, timeFinish;

void timeBegin() {
    timeStart = clock();
}

void timeEnd() {
    timeFinish = clock();
}

void timeDuration() {
    double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
    cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
    cout << " sec " << endl;
}

class InputReader {
public:
    InputReader() {
        input_file = stdin;
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
    inline InputReader &operator >>(long long &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance() {
        ++ cursor;
        if(cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

const int MAXN = 500000;

char s[1 + MAXN], t[1 + MAXN];
vector<int> ones;

int main() {
    //timeBegin();
    //ifstream cin("input.txt");
    //ofstream cout("papagali.out");
    int n;
    cin >> n >> (s + 1) >> (t + 1);
    int s1 = 0, t1 = 0;
    for (int i = 1; i <= n; i++) {
        if (s[i] == '1') {
            s1++;
            ones.push_back(i);
        }
        if (t[i] == '1') {
            t1++;
        }
    }
    reverse(ones.begin(), ones.end());
    if (t1 % 2 != s1 % 2 || t1 > s1) {
        cout << "-1\n";
        return 0;
    }
    long long answer = 0;
    for (int i = 1; i <= n; i++) {
        if (t[i] == '0') {
            if (!ones.empty() && ones.back() <= i) {
                int x = ones.back();
                ones.pop_back();
                if (ones.empty()) {
                    cout << "-1\n";
                    return 0;
                }
                int y = ones.back();
                ones.pop_back();
                answer += y - x;
            }
        } else {
            if (ones.empty()) {
                cout << "-1\n";
                return 0;
            }
            int x = ones.back();
            ones.pop_back();
            answer += x - i;
        }
    }
    cout << answer << "\n";
    return 0;
}


