func_code_string: #include <bits/stdc++.h>   using namespace std;   const int size = 400007;   const long long modulo = 1000000007;   const int INF = 2e9;   const double EPS = 1e-8;   const double PI = acos(-1.0);   int d[2][size];   char board[2][size];   int compo[size];   int l[size], r[size];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     cin.ignore();     gets(board[0]);     gets(board[1]);     for (int i = 0; i < 2; i++)       for (int j = 0; j < size; j++) d[i][j] = 2e9;     int j = 0, com = -1;     vector<bool> start(n, false);     while (j < n) {       while (j < n && board[0][j] ==  X  && board[1][j] ==  X ) ++j;       if (j < n) {         start[j] = true;         ++com;         compo[j] = com;       }       d[0][j] = (board[0][j] ==  X  ? 2e9 : 0);       d[0][j + n] = (board[1][j] ==  X  ? 2e9 : 1);       d[1][j] = (board[0][j] ==  X  ? 2e9 : 1);       d[1][j + n] = (board[1][j] ==  X  ? 2e9 : 0);       int col = j + 1;       while (col < n) {         bool ok = false;         for (int st = 0; st < 2; st++)           for (int cell = col; cell < 2 * n; cell += n)             if (board[cell / n][col] !=  X  &&                 d[st][cell] > d[st][cell - 1] + 1) {               ok = true;               d[st][cell] = d[st][cell - 1] + 1;             }         for (int st = 0; st < 2; st++)           for (int cell = col; cell < 2 * n; cell += n)             if (board[cell / n][col] !=  X  &&                 d[st][cell] > d[st][(cell + n) % (2 * n)] + 1) {               d[st][cell] = d[st][(cell + n) % (2 * n)] + 1;             }         if (ok) {           compo[col] = com;           ++col;         } else {           break;         }       }       j = col;     }     if (board[0][0] ==  .  && board[1][0] ==  . )       l[0] = -1;     else       l[0] = 0;     for (int i = 1; i < n; i++) {       if (board[0][i] ==  .  && board[1][i] ==  . )         l[i] = l[i - 1];       else         l[i] = i;     }     if (board[0][n - 1] ==  .  && board[1][n - 1] ==  . )       r[n - 1] = n;     else       r[n - 1] = n - 1;     for (int i = n - 2; i >= 0; i--) {       if (board[0][i] ==  .  && board[1][i] ==  . )         r[i] = r[i + 1];       else         r[i] = i;     }     while (m--) {       int a, b;       cin >> a >> b;       if (a > b) swap(a, b);       if (a + n == b)         cout << 1 << endl;       else {         --a;         --b;         int a_row = a / n, a_col = a % n, b_row = b / n, b_col = b % n;         if (a_col > b_col) {           swap(a_row, b_row);           swap(a_col, b_col);           swap(a, b);         }         if (compo[a_col] != compo[b_col]) {           cout << -1 << endl;           continue;         }         int ans = min(d[0][b], d[1][b]);         if (l[a_col] == a_col)           ans -= min(d[0][a], d[1][a]);         else if (start[a_col])           ans = d[a_row][b];         else {           if (r[a_col] > b_col)             ans = b_col - a_col + (a_row != b_row);           else {             int right_exit = (board[0][r[a_col]] ==  .  ? 0 : 1);             ans = min(d[0][b] - d[0][a], d[1][b] - d[1][a]);             int pref = (d[0][b] - d[0][a] < d[1][b] - d[1][a] ? 0 : 1);             int lind = l[a_col];             int left_exit = -1;             if (lind < 0 || start[lind + 1])               left_exit = pref;             else if (board[0][lind] ==  . )               left_exit = 0;             else               left_exit = 1;             if (right_exit != a_row && left_exit != a_row) ans += 2;           }         }         cout << ans <<   n ;       }     }     return 0;   }   