#include <bits/stdc++.h>
using namespace std;
const int N = 5, M = 2e5 + 5;
struct Matrix {
  int num[N][N];
  Matrix() {
    memset(num, 0x3f, sizeof(num));
    for (int i = 0; i < N; ++i) num[i][i] = 0;
  }
  int* operator[](int n) { return num[n]; }
};
Matrix mul(Matrix a, Matrix b) {
  Matrix c;
  for (int i = 0; i < N; ++i) c[i][i] = 0x3f3f3f3f;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      for (int k = 0; k < N; ++k) c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
  return c;
}
struct Tree {
  Matrix sum;
} t[M << 2];
int n, q, a[M];
Matrix CAS(int x) {
  Matrix t;
  if (x == 2) t[0][0] = 1, t[1][0] = 0;
  if (x == 0) t[1][1] = 1, t[2][1] = 0;
  if (x == 1) t[2][2] = 1, t[3][2] = 0;
  if (x == 7) t[3][3] = 1, t[4][3] = 0;
  if (x == 6) t[4][4] = 1, t[3][3] = 1;
  return t;
}
void build(int rt, int l, int r) {
  if (l == r) {
    t[rt].sum = CAS(a[l]);
    return;
  }
  int mid = l + r >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  t[rt].sum = mul(t[rt << 1 | 1].sum, t[rt << 1].sum);
}
Matrix query(int rt, int l, int r, int lc, int rc) {
  if (l <= lc && r >= rc) return t[rt].sum;
  int mid = lc + rc >> 1;
  Matrix L, R;
  if (l <= mid) L = query(rt << 1, l, r, lc, mid);
  if (r > mid) R = query(rt << 1 | 1, l, r, mid + 1, rc);
  return mul(R, L);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) scanf("%1d", a + i);
  build(1, 1, n);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    Matrix ans;
    ans = mul(query(1, l, r, 1, n), ans);
    printf("%d\n", ans[4][0] > (r - l + 1) ? -1 : ans[4][0]);
  }
  return 0;
}
