#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
int n, q;
char s[N];
struct martix {
  int b[5][5];
  friend martix operator+(martix A, martix B) {
    martix tmp;
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        tmp.b[i][j] = n;
        for (int k = 0; k < 5; k++)
          tmp.b[i][j] = min(tmp.b[i][j], A.b[i][k] + B.b[k][j]);
      }
    return tmp;
  }
} a[N << 2];
void build(int l, int r, int k) {
  if (l == r) {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) a[k].b[i][j] = i == j ? 0 : n;
    if (s[l] == '2') {
      a[k].b[0][0] = 1;
      a[k].b[0][1] = 0;
    }
    if (s[l] == '0') {
      a[k].b[1][1] = 1;
      a[k].b[1][2] = 0;
    }
    if (s[l] == '1') {
      a[k].b[2][2] = 1;
      a[k].b[2][3] = 0;
    }
    if (s[l] == '7') {
      a[k].b[3][3] = 1;
      a[k].b[3][4] = 0;
    }
    if (s[l] == '6') a[k].b[3][3] = a[k].b[4][4] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, 2 * k);
  build(mid + 1, r, 2 * k + 1);
  a[k] = a[k << 1] + a[k << 1 | 1];
}
martix query(int l, int r, int L, int R, int k) {
  if (l <= L && R <= r) return a[k];
  int mid = (L + R) / 2;
  if (r <= mid)
    return query(l, r, L, mid, 2 * k);
  else if (l > mid)
    return query(l, r, mid + 1, R, 2 * k + 1);
  return query(l, mid, L, mid, 2 * k) +
         query(mid + 1, r, mid + 1, R, 2 * k + 1);
}
int main() {
  while (~scanf("%d%d", &n, &q)) {
    scanf("%s", s + 1);
    build(1, n, 1);
    while (q--) {
      int l, r;
      scanf("%d%d", &l, &r);
      int ans = query(l, r, 1, n, 1).b[0][4];
      if (ans >= n)
        puts("-1");
      else
        printf("%d\n", ans);
    }
  }
  return 0;
}
