#include <bits/stdc++.h>
using namespace std;
int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
struct Matrix {
  int a[5][5];
  Matrix() { memset(a, 0x3f, sizeof(a)); }
  int* operator[](int x) { return a[x]; }
  friend Matrix operator*(Matrix A, Matrix B) {
    Matrix C;
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++) C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
    return C;
  }
};
char s[200000 + 5];
Matrix t[4 * 200000 + 5];
void Build(int u, int L, int R) {
  if (L == R) {
    t[u][0][0] = t[u][1][1] = t[u][2][2] = t[u][3][3] = t[u][4][4] = 0;
    if (s[L] == '2')
      t[u][0][0] = 1, t[u][1][0] = 0;
    else if (s[L] == '0')
      t[u][1][1] = 1, t[u][2][1] = 0;
    else if (s[L] == '1')
      t[u][2][2] = 1, t[u][3][2] = 0;
    else if (s[L] == '7')
      t[u][3][3] = 1, t[u][4][3] = 0;
    else if (s[L] == '6')
      t[u][3][3] = 1, t[u][4][4] = 1;
    return;
  }
  int Mid = (L + R) >> 1;
  Build((u << 1), L, Mid), Build((u << 1 | 1), Mid + 1, R);
  t[u] = t[(u << 1 | 1)] * t[(u << 1)];
  return;
}
Matrix Query(int u, int L, int R, int qL, int qR) {
  if (qL <= L && R <= qR) return t[u];
  int Mid = (L + R) >> 1;
  if (qR <= Mid)
    return Query((u << 1), L, Mid, qL, qR);
  else if (Mid + 1 <= qL)
    return Query((u << 1 | 1), Mid + 1, R, qL, qR);
  return Query((u << 1 | 1), Mid + 1, R, qL, qR) *
         Query((u << 1), L, Mid, qL, qR);
}
int main() {
  int n = read(), q = read();
  scanf("%s", s + 1);
  Build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l = read(), r = read();
    Matrix Ans = Query(1, 1, n, l, r);
    printf("%d\n", Ans[4][0] > (r - l + 1) ? -1 : Ans[4][0]);
  }
  return 0;
}
