#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[200100];
struct node {
  int a[5][5];
  node() {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) a[i][j] = 1e9;
  }
} T[200100 << 2];
char ch[] = {'2', '0', '1', '7', '6'};
int find(char x) {
  for (int i = 0; i < 5; i++)
    if (x == ch[i]) return i;
  return -1;
}
node pushup(node &a, node &b) {
  node res;
  for (int i = 0; i < 5; i++)
    for (int j = i; j < 5; j++)
      for (int k = i; k <= j; k++)
        res.a[i][j] = min(res.a[i][j], a.a[i][k] + b.a[k][j]);
  return res;
}
void build(int p, int l, int r) {
  if (l == r) {
    int f = find(s[l]);
    for (int i = 0; i < 5; i++) T[p].a[i][i] = 0;
    if (f != -1 && f < 4) {
      T[p].a[f][f + 1] = 0;
      T[p].a[f][f] = 1;
    } else if (f == 4)
      T[p].a[3][3] = T[p].a[4][4] = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build((p << 1), l, mid);
  build((p << 1 | 1), mid + 1, r);
  T[p] = pushup(T[(p << 1)], T[(p << 1 | 1)]);
}
node query(int p, int l, int r, int ql, int qr) {
  if (ql == l && qr == r) return T[p];
  int mid = (l + r) >> 1;
  if (qr <= mid) return query((p << 1), l, mid, ql, qr);
  if (ql > mid)
    return query((p << 1 | 1), mid + 1, r, ql, qr);
  else {
    node tmpl = query((p << 1), l, mid, ql, mid);
    node tmpr = query((p << 1 | 1), mid + 1, r, mid + 1, qr);
    return pushup(tmpl, tmpr);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  build(1, 1, n);
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ans = query(1, 1, n, l, r).a[0][4];
    if (ans == 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
