#include <bits/stdc++.h>
using namespace std;
const int base = 13331;
const long long p1 = 998244353;
const long long p2 = 1e9 + 7;
const long long INF = 0x3f3f3f3f;
const int N = 2e5 + 10, M = 1e4 + 10;
const int mod = 1000000009;
inline long long read() {
  char c = getchar();
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
struct node {
  int g[5][5];
  node() { memset(g, INF, sizeof g); }
};
node operator+(node a, node b) {
  node res;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++)
        res.g[i][j] = min(res.g[i][j], a.g[i][k] + b.g[k][j]);
  return res;
}
node tr[N << 2];
char s[N];
void build(int o, int l, int r) {
  if (l == r) {
    for (int i = 0; i < 5; i++) tr[o].g[i][i] = 0;
    if (s[l] == '2') tr[o].g[0][0] = 1, tr[o].g[0][1] = 0;
    if (s[l] == '0') tr[o].g[1][1] = 1, tr[o].g[1][2] = 0;
    if (s[l] == '1') tr[o].g[2][2] = 1, tr[o].g[2][3] = 0;
    if (s[l] == '7') tr[o].g[3][3] = 1, tr[o].g[3][4] = 0;
    if (s[l] == '6') tr[o].g[3][3] = 1, tr[o].g[4][4] = 1;
    return;
  }
  int mid = l + r >> 1;
  build((o << 1), l, mid);
  build((o << 1 | 1), mid + 1, r);
  tr[o] = tr[(o << 1)] + tr[(o << 1 | 1)];
  return;
}
node qry(int o, int l, int r, int L, int R) {
  if (l >= L && r <= R) return tr[o];
  int mid = l + r >> 1;
  if (R <= mid) return qry((o << 1), l, mid, L, R);
  if (L > mid) return qry((o << 1 | 1), mid + 1, r, L, R);
  return qry((o << 1), l, mid, L, R) + qry((o << 1 | 1), mid + 1, r, L, R);
}
int main() {
  int n = read(), q = read();
  scanf("%s", s + 1);
  build(1, 1, n);
  while (q--) {
    int l = read(), r = read();
    int t = qry(1, 1, n, l, r).g[0][4];
    printf("%d\n", t == INF ? -1 : t);
  }
  return 0;
}
