#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int maxL = 5;
const int inf = 1e9;
int n, Q;
char s[maxn];
int L = 5;
struct Matrix {
  int x[maxL][maxL];
} I, INF, A, tr[maxn << 2];
Matrix operator*(const Matrix a, const Matrix b) {
  Matrix ret = INF;
  for (int i = 0; i < L; i++)
    for (int j = 0; j < L; j++)
      for (int k = 0; k < L; k++)
        ret.x[i][j] = min(ret.x[i][j], a.x[i][k] + b.x[k][j]);
  return ret;
}
void build(int rt = 1, int l = 1, int r = n) {
  if (l == r) {
    tr[rt] = INF;
    for (int i = 0; i < L; i++) tr[rt].x[i][i] = 0;
    if (s[l] == '2') tr[rt].x[0][0] = 1, tr[rt].x[0][1] = 0;
    if (s[l] == '0') tr[rt].x[1][1] = 1, tr[rt].x[1][2] = 0;
    if (s[l] == '1') tr[rt].x[2][2] = 1, tr[rt].x[2][3] = 0;
    if (s[l] == '7') tr[rt].x[3][3] = 1, tr[rt].x[3][4] = 0;
    if (s[l] == '6') tr[rt].x[3][3] = 1, tr[rt].x[4][4] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  tr[rt] = tr[rt << 1] * tr[rt << 1 | 1];
}
void qry(int nl, int nr, int rt = 1, int l = 1, int r = n) {
  if (nl <= l && nr >= r) {
    A = A * tr[rt];
    return;
  }
  int mid = l + r >> 1;
  if (mid >= nl) qry(nl, nr, rt << 1, l, mid);
  if (mid + 1 <= nr) qry(nl, nr, rt << 1 | 1, mid + 1, r);
  return;
}
int main() {
  for (int i = 0; i < L; i++)
    for (int j = 0; j < L; j++) INF.x[i][j] = inf;
  scanf("%d %d %s", &n, &Q, s + 1);
  build();
  while (Q--) {
    int u, v;
    scanf("%d %d", &u, &v);
    A = INF;
    A.x[0][0] = 0;
    qry(u, v);
    if (A.x[0][4] == inf)
      puts("-1");
    else
      printf("%d\n", A.x[0][4]);
  }
  return 0;
}
