413_E. Maze 2D_17189
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
#include <bits/stdc++.h>
using namespace std;
string s[2];
int comp[2][1000000];
int vis[2][1000000];
int n;
void dfs(int i, int j, int col) {
  if (i >= 2 || j >= n || i < 0 || j < 0) return;
  if (s[i][j] == 'X') return;
  if (vis[i][j]) return;
  vis[i][j] = 1;
  comp[i][j] = col;
  dfs(i + 1, j, col);
  dfs(i, j + 1, col);
  dfs(i, j - 1, col);
  dfs(i - 1, j, col);
}
int L[1000000];
int R[1000000];
int sum[1000000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> n >> q;
  int V = 0;
  cin >> s[0] >> s[1];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      if (!vis[i][j]) dfs(i, j, ++V);
    }
  }
  if ((s[0][0] == 'X') + (s[1][0] == 'X') == 1)
    L[0] = 0;
  else
    L[0] = -1;
  if ((s[0].back() == 'X') + (s[1].back() == 'X') == 1)
    R[n - 1] = n - 1;
  else
    R[n - 1] = n + 1;
  for (int i = 1; i < n; i++) {
    L[i] = L[i - 1];
    if ((s[0][i] == 'X') + (s[1][i] == 'X') == 1) L[i] = i;
  }
  for (int i = n - 2; i >= 0; i--) {
    R[i] = R[i + 1];
    if ((s[0][i] == 'X') + (s[1][i] == 'X') == 1) R[i] = i;
  }
  for (int i = 0; i < n; i++) {
    if (i) sum[i] = sum[i - 1];
    if (i && L[i] == i && L[i - 1] != -1 && s[0][L[i - 1]] != s[0][i]) sum[i]++;
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    int x1 = (x - 1) / n;
    int y1 = (x - 1) % n;
    int x2 = (y - 1) / n;
    int y2 = (y - 1) % n;
    x = y1;
    y = y2;
    if (y1 > y2) {
      swap(y1, y2);
      swap(x1, x2);
      swap(x, y);
    }
    if (comp[x1][y1] != comp[x2][y2]) {
      cout << "-1\n";
    } else {
      if (R[x] > L[y])
        cout << y - x + abs(x1 - x2) << endl;
      else {
        int add = 0;
        if (x1 == 0 && s[0][R[x]] == 'X') add++;
        if (x1 == 1 && s[1][R[x]] == 'X') add++;
        if (x2 == 0 && s[0][L[y]] == 'X') add++;
        if (x2 == 1 && s[1][L[y]] == 'X') add++;
        cout << y - x + sum[L[y]] - sum[R[x]] + add << endl;
      }
    }
  }
}
