#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
char A[2][300000];
int a[2][300000];
long long d[300000][20][2][2];
long long e[30][2];
int main() {
  int i, j, k, l;
  scanf("%d%d", &n, &m);
  scanf("%s%s", A[0], A[1]);
  for (i = 0; i < 2; i++) {
    for (j = 0; j < n; j++) {
      if (A[i][j] == 'X') a[i][j] = 1;
    }
  }
  a[0][n] = a[1][n] = 1;
  for (i = 0; i < n; i++) {
    if (!a[0][i] && !a[0][i + 1]) d[i][0][0][0] = 1;
    if (!a[0][i] && !a[1][i + 1] && (!a[1][i] || !a[0][i + 1]))
      d[i][0][0][1] = 2;
    if (!a[1][i] && !a[0][i + 1] && (!a[0][i] || !a[1][i + 1]))
      d[i][0][1][0] = 2;
    if (!a[1][i] && !a[1][i + 1]) d[i][0][1][1] = 1;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 2; k++) {
        if (d[i][0][j][k] == 0) d[i][0][j][k] = 300000;
      }
    }
  }
  for (j = 1; j < 20; j++) {
    for (i = 0; i < n; i++) {
      for (k = 0; k < 2; k++) {
        for (l = 0; l < 2; l++) {
          if (i + (1 << (j - 1)) > n) continue;
          d[i][j][k][l] =
              min(d[i][j - 1][k][0] + d[i + (1 << (j - 1))][j - 1][0][l],
                  d[i][j - 1][k][1] + d[i + (1 << (j - 1))][j - 1][1][l]);
        }
      }
    }
  }
  for (i = 0; i < m; i++) {
    int x, y;
    int a1, a2, b1, b2;
    scanf("%d%d", &x, &y);
    x--, y--;
    a1 = x % n, b1 = y % n;
    a2 = x >= n, b2 = y >= n;
    if (a1 == b1 && a2 == b2) {
      printf("0\n");
      continue;
    } else if (a1 == b1) {
      printf("1\n");
      continue;
    }
    if (a1 > b1) swap(a1, b1), swap(a2, b2);
    memset(e, 0, sizeof(e));
    e[0][a2] = 0;
    if (a[1 - a2][a1] == 0)
      e[0][1 - a2] = 1;
    else
      e[0][1 - a2] = 300000;
    int cha = b1 - a1;
    int now = a1;
    int xnow = 0;
    for (j = 0; j < 20; j++) {
      if (cha & (1 << j)) {
        e[xnow + 1][0] =
            min(e[xnow][0] + d[now][j][0][0], e[xnow][1] + d[now][j][1][0]);
        e[xnow + 1][1] =
            min(e[xnow][0] + d[now][j][0][1], e[xnow][1] + d[now][j][1][1]);
        now = now + (1 << j);
        xnow++;
      }
    }
    if (e[xnow][b2] >= 300000)
      printf("-1\n");
    else
      printf("%I64d\n", e[xnow][b2]);
  }
  return 0;
}

### Prompt

Develop a solution in CPP to the problem described below:

The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2 × n maze.

Imagine a maze that looks like a 2 × n rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.

Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2 × n maze.

Input

The first line contains two integers, n and m (1 ≤ n ≤ 2·105; 1 ≤ m ≤ 2·105) — the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle).

Each of the next m lines contains two integers vi and ui (1 ≤ vi, ui ≤ 2n) — the description of the i-th request. Numbers vi, ui mean that you need to print the value of the shortest path from the cell of the maze number vi to the cell number ui. We assume that the cells of the first line of the maze are numbered from 1 to n, from left to right, and the cells of the second line are numbered from n + 1 to 2n from left to right. It is guaranteed that both given cells are empty.

Output

Print m lines. In the i-th line print the answer to the i-th request — either the size of the shortest path or -1, if we can't reach the second cell from the first one.

Examples

Input

4 7
.X..
...X
5 1
1 3
7 7
1 4
6 1
4 7
5 7


Output

1
4
0
5
2
2
2


Input

10 3
X...X..X..
..X...X..X
11 7
7 18
18 10


Output

9
-1
3

### Response

```cpp
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
char A[2][300000];
int a[2][300000];
long long d[300000][20][2][2];
long long e[30][2];
int main() {
  int i, j, k, l;
  scanf("%d%d", &n, &m);
  scanf("%s%s", A[0], A[1]);
  for (i = 0; i < 2; i++) {
    for (j = 0; j < n; j++) {
      if (A[i][j] == 'X') a[i][j] = 1;
    }
  }
  a[0][n] = a[1][n] = 1;
  for (i = 0; i < n; i++) {
    if (!a[0][i] && !a[0][i + 1]) d[i][0][0][0] = 1;
    if (!a[0][i] && !a[1][i + 1] && (!a[1][i] || !a[0][i + 1]))
      d[i][0][0][1] = 2;
    if (!a[1][i] && !a[0][i + 1] && (!a[0][i] || !a[1][i + 1]))
      d[i][0][1][0] = 2;
    if (!a[1][i] && !a[1][i + 1]) d[i][0][1][1] = 1;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 2; k++) {
        if (d[i][0][j][k] == 0) d[i][0][j][k] = 300000;
      }
    }
  }
  for (j = 1; j < 20; j++) {
    for (i = 0; i < n; i++) {
      for (k = 0; k < 2; k++) {
        for (l = 0; l < 2; l++) {
          if (i + (1 << (j - 1)) > n) continue;
          d[i][j][k][l] =
              min(d[i][j - 1][k][0] + d[i + (1 << (j - 1))][j - 1][0][l],
                  d[i][j - 1][k][1] + d[i + (1 << (j - 1))][j - 1][1][l]);
        }
      }
    }
  }
  for (i = 0; i < m; i++) {
    int x, y;
    int a1, a2, b1, b2;
    scanf("%d%d", &x, &y);
    x--, y--;
    a1 = x % n, b1 = y % n;
    a2 = x >= n, b2 = y >= n;
    if (a1 == b1 && a2 == b2) {
      printf("0\n");
      continue;
    } else if (a1 == b1) {
      printf("1\n");
      continue;
    }
    if (a1 > b1) swap(a1, b1), swap(a2, b2);
    memset(e, 0, sizeof(e));
    e[0][a2] = 0;
    if (a[1 - a2][a1] == 0)
      e[0][1 - a2] = 1;
    else
      e[0][1 - a2] = 300000;
    int cha = b1 - a1;
    int now = a1;
    int xnow = 0;
    for (j = 0; j < 20; j++) {
      if (cha & (1 << j)) {
        e[xnow + 1][0] =
            min(e[xnow][0] + d[now][j][0][0], e[xnow][1] + d[now][j][1][0]);
        e[xnow + 1][1] =
            min(e[xnow][0] + d[now][j][0][1], e[xnow][1] + d[now][j][1][1]);
        now = now + (1 << j);
        xnow++;
      }
    }
    if (e[xnow][b2] >= 300000)
      printf("-1\n");
    else
      printf("%I64d\n", e[xnow][b2]);
  }
  return 0;
}

```