#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 10;
const int mxt = (mx << 2);
const int oo = 1e9;
char s[mx];
struct node {
  int f[5][5];
};
node st[mxt];
string w = "2017x";
node mrg(node x, node y) {
  node ans;
  for (int a = 0; a <= 4; a++) {
    for (int b = 0; b <= 4; b++) {
      ans.f[a][b] = oo;
      for (int c = 0; c <= 4; c++) {
        ans.f[a][b] = min(ans.f[a][b], x.f[a][c] + y.f[c][b]);
      }
    }
  }
  return ans;
}
void init(int u, int l, int r) {
  if (l == r) {
    for (int a = 0; a <= 4; a++) {
      for (int b = 0; b <= 4; b++) st[u].f[a][b] = oo;
      st[u].f[a][a] = (s[l] == w[a]);
      if (a + 1 <= 4 && s[l] == w[a]) {
        st[u].f[a][a + 1] = 0;
      }
      if (a >= 3 && s[l] == '6') st[u].f[a][a] = 1;
    }
  } else {
    int mid = (l + r) >> 1;
    init(u + u + 1, l, mid);
    init(u + u + 2, mid + 1, r);
    st[u] = mrg(st[u + u + 1], st[u + u + 2]);
  }
}
node qry(int u, int l, int r, int f, int t) {
  if (l == f && r == t) return st[u];
  int mid = (l + r) >> 1;
  if (t <= mid) return qry(u + u + 1, l, mid, f, t);
  if (f > mid) return qry(u + u + 2, mid + 1, r, f, t);
  return mrg(qry(u + u + 1, l, mid, f, mid),
             qry(u + u + 2, mid + 1, r, mid + 1, t));
}
int main() {
  int n, q, l, r;
  scanf("%d %d %s", &n, &q, s);
  init(0, 0, n - 1);
  while (q--) {
    scanf("%d %d", &l, &r);
    node ans = qry(0, 0, n - 1, --l, --r);
    int o = ans.f[0][4];
    if (o >= oo) o = -1;
    printf("%d\n", o);
  }
  return 0;
}
