#include <bits/stdc++.h>
using namespace std;
const int T = (1 << 18);
const int INF = 1000000000;
char inp[T];
struct MAT {
  int data[5][5];
};
MAT Z, I, matrices[10], seg[2 * T];
MAT operator*(const MAT &A, const MAT &B) {
  MAT C = Z;
  for (int i = 0; i < 5; ++i) {
    for (int k = 0; k < 5; ++k) {
      for (int j = 0; j < 5; ++j) {
        C.data[i][j] = max(C.data[i][j], A.data[i][k] + B.data[k][j]);
      }
    }
  }
  return C;
}
void init() {
  for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 5; ++j) {
      Z.data[i][j] = I.data[i][j] = -INF;
    }
    I.data[i][i] = 0;
  }
  for (int i = 0; i < 10; ++i) {
    matrices[i] = I;
  }
  matrices[2].data[0][0] = -1;
  matrices[2].data[1][0] = 0;
  matrices[0].data[1][1] = -1;
  matrices[0].data[2][1] = 0;
  matrices[1].data[2][2] = -1;
  matrices[1].data[3][2] = 0;
  matrices[7].data[3][3] = -1;
  matrices[7].data[4][3] = 0;
  matrices[6].data[3][3] = -1;
  matrices[6].data[4][4] = -1;
}
MAT seg_que(int root, int lm, int rm, int u, int v) {
  if (u <= lm && rm <= v) return seg[root];
  int mid = (lm + rm) / 2;
  MAT lv = I, rv = I;
  if (u <= mid) lv = seg_que(2 * root, lm, mid, u, v);
  if (mid < v) rv = seg_que(2 * root + 1, mid + 1, rm, u, v);
  return rv * lv;
}
int main() {
  init();
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", inp);
  for (int i = 0; i < n; ++i) {
    seg[T + i] = matrices[inp[i] - '0'];
  }
  for (int i = T + n; i < 2 * T; ++i) {
    seg[i] = I;
  }
  for (int i = T - 1; i >= 1; --i) seg[i] = seg[2 * i + 1] * seg[2 * i];
  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    MAT ans = seg_que(1, T, 2 * T - 1, T + a, T + b);
    int fans = -ans.data[4][0];
    if (fans == INF) fans = -1;
    printf("%d\n", fans);
  }
  return 0;
}
