#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int NSTATE = 5;
typedef int Mat[NSTATE][NSTATE];
int n, nq;
char s[MAXN + 1];
Mat sval[4 * MAXN];
void scopy(Mat &A, Mat &B) {
  for (int i = (0); i < (NSTATE); ++i)
    for (int j = (i); j < (NSTATE); ++j) A[i][j] = B[i][j];
}
void smult(Mat &A, Mat &B) {
  Mat C;
  for (int i = (0); i < (NSTATE); ++i)
    for (int j = (i); j < (NSTATE); ++j) {
      int cur = INT_MAX;
      for (int k = (i); k <= (j); ++k)
        if (A[i][k] != INT_MAX && B[k][j] != INT_MAX && A[i][k] + B[k][j] < cur)
          cur = A[i][k] + B[k][j];
      C[i][j] = cur;
    }
  scopy(A, C);
}
void sinit(int x, int l, int r) {
  if (l == r) {
    for (int i = (0); i < (NSTATE); ++i)
      for (int j = (0); j < (NSTATE); ++j) sval[x][i][j] = INT_MAX;
    sval[x][0][0] = s[l] == '2' ? 1 : 0;
    if (s[l] == '2') sval[x][0][1] = 0;
    sval[x][1][1] = s[l] == '0' ? 1 : 0;
    if (s[l] == '0') sval[x][1][2] = 0;
    sval[x][2][2] = s[l] == '1' ? 1 : 0;
    if (s[l] == '1') sval[x][2][3] = 0;
    sval[x][3][3] = s[l] == '6' || s[l] == '7' ? 1 : 0;
    if (s[l] == '7') sval[x][3][4] = 0;
    sval[x][4][4] = s[l] == '6' ? 1 : 0;
  } else {
    int m = l + (r - l) / 2;
    sinit(2 * x + 1, l, m);
    sinit(2 * x + 2, m + 1, r);
    scopy(sval[x], sval[2 * x + 1]);
    smult(sval[x], sval[2 * x + 2]);
  }
}
void sget(int x, int l, int r, int L, int R, Mat &RES) {
  if (L <= l && r <= R) {
    smult(RES, sval[x]);
  } else {
    int m = l + (r - l) / 2;
    if (L <= m) sget(2 * x + 1, l, m, L, R, RES);
    if (m + 1 <= R) sget(2 * x + 2, m + 1, r, L, R, RES);
  }
}
void run() {
  scanf("%d%d%s", &n, &nq, s);
  sinit(0, 0, n - 1);
  for (int i = (0); i < (nq); ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l, --r;
    Mat res;
    for (int i = (0); i < (NSTATE); ++i)
      for (int j = (0); j < (NSTATE); ++j) res[i][j] = i == j ? 0 : INT_MAX;
    sget(0, 0, n - 1, l, r, res);
    int ret = res[0][4];
    printf("%d\n", ret == INT_MAX ? -1 : ret);
  }
}
int main() {
  run();
  return 0;
}
