#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:16777216")
const int inf = 0x3f3f3f3f;
const string debug_line = "yolo";
const double PI = 4 * atan(1);
typedef struct node2 {
  long long d[2][2];
} node1;
node1 tree[800001];
char s[2][200001];
bool initialise(int node, int b, int e) {
  if (b > e) {
    return false;
  } else if (b == e) {
    if (s[0][b] == 'X') {
      tree[node].d[0][0] = 100000000;
    } else {
      tree[node].d[0][0] = 0;
    }
    if (s[1][b] == 'X') {
      tree[node].d[1][1] = 100000000;
    } else {
      tree[node].d[1][1] = 0;
    }
    if (s[0][b] == 'X' || s[1][b] == 'X') {
      tree[node].d[1][0] = tree[node].d[0][1] = 100000000;
    } else {
      tree[node].d[1][0] = tree[node].d[0][1] = 1;
    }
    return true;
  } else {
    bool temp1 = initialise(2 * node, b, (b + e) / 2);
    bool temp2 = initialise(2 * node + 1, (b + e) / 2 + 1, e);
    if (temp1 && !temp2) {
      tree[node] = tree[2 * node];
      return true;
    }
    if (!temp1 && temp2) {
      tree[node] = tree[2 * node + 1];
      return true;
    }
    for (int(i) = (0); (i) < (2); ++(i)) {
      for (int(j) = (0); (j) < (2); ++(j)) {
        tree[node].d[i][j] = INT_MAX;
        for (int(k) = (0); (k) < (2); ++(k)) {
          tree[node].d[i][j] =
              min(tree[node].d[i][j],
                  tree[2 * node].d[i][k] + tree[2 * node + 1].d[k][j] + 1);
        }
      }
    }
    return true;
  }
}
bool query(node1 &res, int node, int b, int e, int i, int j) {
  if (b > e || i > e || j < b) {
    return false;
  } else if (i <= b && e <= j) {
    res = tree[node];
    return true;
  } else {
    node1 l, r;
    l.d[0][0] = l.d[0][1] = l.d[1][0] = l.d[1][1] = INT_MAX;
    r.d[0][0] = r.d[0][1] = r.d[1][0] = r.d[1][1] = INT_MAX;
    bool temp1 = query(l, 2 * node, b, (b + e) / 2, i, j);
    bool temp2 = query(r, 2 * node + 1, (b + e) / 2 + 1, e, i, j);
    if (!temp1 && temp2) {
      res = r;
      return true;
    }
    if (!temp2 && temp1) {
      res = l;
      return true;
    }
    for (int(i) = (0); (i) < (2); ++(i)) {
      for (int(j) = (0); (j) < (2); ++(j)) {
        for (int(k) = (0); (k) < (2); ++(k)) {
          res.d[i][j] = min(res.d[i][j], l.d[i][k] + r.d[k][j] + 1);
        }
      }
    }
    return true;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  cin >> s[0];
  cin >> s[1];
  initialise(1, 0, n - 1);
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    node1 res;
    int q1 = 0;
    int q2 = 0;
    res.d[0][0] = res.d[1][0] = res.d[1][1] = res.d[0][1] = 100000000;
    query(res, 1, 0, n - 1, min(l % n, r % n), max(r % n, l % n));
    if (l % n < r % n) {
      if (l >= n) {
        q1 = 1;
      }
      if (r >= n) {
        q2 = 1;
      }
    } else {
      if (r >= n) {
        q1 = 1;
      }
      if (l >= n) {
        q2 = 1;
      }
    }
    if (res.d[q1][q2] >= 100000000) {
      printf("-1\n");
    } else {
      printf("%lld\n", res.d[q1][q2]);
    }
  }
  return 0;
}
