#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 1e3;
struct Node {
  int a[5][5];
  int* operator[](int x) { return a[x]; }
  Node operator+(Node b) {
    Node c;
    memset(c.a, 0x3f, sizeof(c.a));
    for (int i = 0; i < 5; ++i)
      for (int j = 0; j < 5; ++j)
        for (int k = 0; k < 5; ++k) c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
    return c;
  }
} tr[MAXN << 2];
char str[MAXN];
int sl, sr;
Node build(int id, int l, int r) {
  if (l == r) {
    Node& e = tr[id];
    for (int i = 0; i < 5; ++i)
      for (int j = 0; j < 5; ++j) e[i][j] = (i == j) ? 0 : 0x3f3f3f3f;
    switch (str[l]) {
      case '2':
        e[0][0] = 1;
        e[0][1] = 0;
        break;
      case '0':
        e[1][1] = 1;
        e[1][2] = 0;
        break;
      case '1':
        e[2][2] = 1;
        e[2][3] = 0;
        break;
      case '7':
        e[3][3] = 1;
        e[3][4] = 0;
        break;
      case '6':
        e[3][3] = 1;
        e[4][4] = 1;
        break;
    }
  } else {
    int m = l + r >> 1;
    tr[id] = build(id << 1, l, m) + build(id << 1 | 1, m + 1, r);
  }
  return tr[id];
}
Node query(int id, int l, int r) {
  if (sl <= l && r <= sr) return tr[id];
  int m = l + r >> 1;
  if (m >= sr) return query(id << 1, l, m);
  if (m < sl) return query(id << 1 | 1, m + 1, r);
  return query(id << 1, l, m) + query(id << 1 | 1, m + 1, r);
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  scanf("%s", str + 1);
  build(1, 1, n);
  while (q--) {
    scanf("%d%d", &sl, &sr);
    int ans = query(1, 1, n)[0][4];
    if (ans == 0x3f3f3f3f)
      puts("-1");
    else
      printf("%d\n", ans);
  }
  return 0;
}
