#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int maxn = 200004;
string yr = "2017y";
char str[maxn];
struct node {
  int a[5][5];
  node() {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) a[i][j] = inf;
  }
} emp;
node seg[4 * maxn];
node Merge(node p, node q) {
  node c;
  for (int i = 0; i < 5; i++) {
    for (int j = i; j < 5; j++) {
      for (int k = i; k <= j; k++)
        c.a[i][j] = min(c.a[i][j], p.a[i][k] + q.a[k][j]);
    }
  }
  return c;
}
void build(int v, int l, int r) {
  if (l > r) return;
  if (l == r) {
    for (int i = 0; i < 5; i++) {
      if (str[l] == yr[i]) {
        seg[v].a[i][i + 1] = 0;
        seg[v].a[i][i] = 1;
      } else
        seg[v].a[i][i] = 0;
    }
    if (str[l] == '6') {
      seg[v].a[3][3] = 1;
      seg[v].a[4][4] = 1;
    }
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  seg[v] = Merge(seg[2 * v], seg[2 * v + 1]);
}
node query(int v, int l, int r, int ll, int rr) {
  if (l > r || ll > r || rr < l) return emp;
  int mid = (l + r) / 2;
  if (ll <= l && r <= rr)
    return seg[v];
  else
    return (Merge(query(2 * v, l, mid, ll, rr),
                  query(2 * v + 1, mid + 1, r, ll, rr)));
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", str + 1);
  for (int i = 0; i < 5; i++) emp.a[i][i] = 0;
  build(1, 1, n);
  int l, r;
  while (q--) {
    scanf("%d %d", &l, &r);
    node ans = query(1, 1, n, l, r);
    if (ans.a[0][4] == inf)
      printf("-1\n");
    else
      printf("%d\n", ans.a[0][4]);
  }
  return 0;
}
