#include <bits/stdc++.h>
using namespace std;
struct Mat {
  int Nxt[5][5];
  Mat() { memset(Nxt, 0x3f3f3f3f, sizeof(Nxt)); }
};
Mat Merge(const Mat &l, const Mat &r) {
  Mat tmp;
  for (int i = (0), _t = (4); i <= _t; ++i)
    for (int j = (i), _t = (4); j <= _t; ++j)
      for (int k = (j), _t = (4); k <= _t; ++k)
        tmp.Nxt[i][k] = min(tmp.Nxt[i][k], l.Nxt[i][j] + r.Nxt[j][k]);
  return tmp;
}
Mat sum[200005 << 2];
char str[200005];
void build(int L, int R, int p) {
  if (L == R) {
    for (int i = (0), _t = (4); i <= _t; ++i) sum[p].Nxt[i][i] = 0;
    if (str[L] == '2') {
      sum[p].Nxt[0][1] = 0;
      sum[p].Nxt[0][0] = 1;
    }
    if (str[L] == '0') {
      sum[p].Nxt[1][2] = 0;
      sum[p].Nxt[1][1] = 1;
    }
    if (str[L] == '1') {
      sum[p].Nxt[2][3] = 0;
      sum[p].Nxt[2][2] = 1;
    }
    if (str[L] == '7') {
      sum[p].Nxt[3][4] = 0;
      sum[p].Nxt[3][3] = 1;
    }
    if (str[L] == '6') {
      sum[p].Nxt[3][3] = 1;
      sum[p].Nxt[4][4] = 1;
    }
    return;
  }
  int mid = L + R >> 1;
  build(L, mid, p << 1);
  build(mid + 1, R, p << 1 | 1);
  sum[p] = Merge(sum[p << 1], sum[p << 1 | 1]);
}
Mat Query(int L, int R, int p, int l, int r) {
  if (L == l && R == r) return sum[p];
  int mid = L + R >> 1;
  if (r <= mid)
    return Query(L, mid, p << 1, l, r);
  else if (l > mid)
    return Query(mid + 1, R, p << 1 | 1, l, r);
  else
    return Merge(Query(L, mid, p << 1, l, mid),
                 Query(mid + 1, R, p << 1 | 1, mid + 1, r));
}
int main() {
  int n, m;
  scanf("%d%d%s", &n, &m, str + 1);
  build(1, n, 1);
  for (int i = (1), _t = (m); i <= _t; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    Mat res = Query(1, n, 1, l, r);
    printf("%d\n", res.Nxt[0][4] == 0x3f3f3f3f ? -1 : res.Nxt[0][4]);
  }
  return 0;
}
