#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
int MOD = 1000000007;
int MAXINT = 2147483647;
long long INFLL = 1000000000000000000LL;
long long MAXLL = 9223372036854775807LL;
int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int my[8] = {0, 0, -1, 1, -1, 1, -1, 1};
char x[3][200005];
int C = 500;
int sav[2][2][200005];
int vs[2][200005];
pair<pair<int, int>, int> v[200005];
bool block[400005];
int ans[200005];
int n, m;
bool cf(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first % n != b.first.first % n)
    return a.first.first % n < b.first.first % n;
  else
    return a.second < b.second;
}
int path(int l, int r, int vs[][200005]) {
  int lol1 = l % n;
  int lol2 = r % n;
  int r1 = l / n;
  vs[r1][lol1] = 0;
  if (x[1 - r1][lol1] == '.')
    vs[1 - r1][lol1] = 1;
  else
    vs[1 - r1][lol1] = INF;
  for (int(a) = (lol1 + 1); (a) <= (lol2); (a)++) {
    for (int(b) = (0); (b) <= (1); (b)++) {
      vs[b][a] = INF;
      if (x[b][a] == '.') {
        if (x[b][a] == '.')
          vs[b][a] = (vs[b][a]) = min((vs[b][a]), (vs[b][a - 1] + 1));
        if (x[1 - b][a] == '.')
          (vs[b][a]) = min((vs[b][a]), (vs[1 - b][a - 1] + 2));
      }
    }
  }
  return vs[r / n][r % n];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(a) = (0); (a) <= (1); (a)++) {
    getchar();
    for (int(b) = (0); (b) <= (n - 1); (b)++) {
      scanf("%c", &x[a][b]);
      if (x[a][b] == 'X') block[n * a + b] = 1;
    }
  }
  for (int(a) = (1); (a) <= (m); (a)++) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    if (l % n > r % n) swap(l, r);
    v[a] = make_pair(make_pair(l, r), a);
  }
  sort(v + 1, v + m + 1, cf);
  int cur = -C;
  for (int(a) = (1); (a) <= (m); (a)++) {
    int l = v[a].first.first;
    int r = v[a].first.second;
    int id = v[a].second;
    bool change = 0;
    while (cur < l % n) {
      change = 1;
      cur += C;
    }
    if (change) {
      queue<int> q;
      if (x[0][cur] == '.') {
        path(cur, n - 1, sav[0]);
      }
      if (x[1][cur] == '.') {
        path(cur + n, n - 1, sav[1]);
      }
    }
    if (r % n < cur) {
      ans[id] = path(l, r, vs);
    } else {
      ans[id] = min(path(l, cur, vs) + sav[0][r / n][r % n],
                    path(l, cur + n, vs) + sav[1][r / n][r % n]);
    }
  }
  for (int(a) = (1); (a) <= (m); (a)++) {
    if (ans[a] >= INF)
      printf("-1\n");
    else
      printf("%d\n", ans[a]);
  }
}
