#include <bits/stdc++.h>
const int MAXN = 2e5 + 5;
struct Node {
  int a[5][5];
  Node(int x = 0) { memset(a, 0, sizeof(a)); }
  inline Node operator*(const Node &t) const {
    Node res;
    for (int i = 0; i <= 4; ++i) {
      for (int j = 0; j <= 4; ++j) {
        res.a[i][j] = -1e9;
        for (int k = 0; k <= 4; ++k) {
          if (a[i][k] == -1e9 || t.a[k][j] == -1e9) continue;
          res.a[i][j] = std::max(res.a[i][j], a[i][k] + t.a[k][j]);
        }
      }
    }
    return res;
  }
  inline void print() {
    for (int i = 0; i <= 4; ++i)
      for (int j = 0; j <= 4; ++j)
        printf("%d%c", a[i][j] == -1e9 ? -1 : a[i][j], " \n"[j == 4]);
  }
} sm[MAXN << 2];
int a[MAXN], ps[MAXN];
inline void build(int x, int l, int r) {
  if (l == r) {
    ps[l] = x;
    for (int i = 0; i <= 4; ++i)
      for (int j = 0; j <= 4; ++j) sm[x].a[i][j] = -1e9;
    for (int i = 0; i <= 4; ++i) sm[x].a[i][i] = 1;
    if (a[l] == 2) {
      sm[x].a[0][0] = 0;
      sm[x].a[1][0] = 1;
    }
    if (a[l] == 0) {
      sm[x].a[1][1] = 0;
      sm[x].a[2][1] = 1;
    }
    if (a[l] == 1) {
      sm[x].a[2][2] = 0;
      sm[x].a[3][2] = 1;
    }
    if (a[l] == 7) {
      sm[x].a[3][3] = 0;
      sm[x].a[4][3] = 1;
    }
    if (a[l] == 6) {
      sm[x].a[3][3] = 0;
      sm[x].a[4][4] = 0;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(((x) << 1), l, mid);
  build(((x) << 1 | 1), mid + 1, r);
  sm[x] = sm[((x) << 1 | 1)] * sm[((x) << 1)];
}
inline Node query(int x, int l, int r, int L, int R) {
  if (l == L && r == R) return sm[x];
  int mid = (l + r) >> 1;
  if (R <= mid) return query(((x) << 1), l, mid, L, R);
  if (L > mid) return query(((x) << 1 | 1), mid + 1, r, L, R);
  return query(((x) << 1 | 1), mid + 1, r, mid + 1, R) *
         query(((x) << 1), l, mid, L, mid);
}
char str[MAXN];
int n, q;
int main() {
  scanf("%d%d", &n, &q);
  scanf("%s", str + 1);
  for (int i = 1; i <= n; ++i) a[i] = str[i] - '0';
  build(1, 1, n);
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ans = query(1, 1, n, l, r).a[4][0];
    printf("%d\n", ans == -1e9 ? -1 : r - l + 1 - ans);
  }
  return 0;
}
