#include <bits/stdc++.h>
using namespace std;
inline vector<vector<int> > merge(const vector<vector<int> >& a,
                                  const vector<vector<int> >& b) {
  if (a.empty()) return b;
  if (b.empty()) return a;
  vector<vector<int> > dp;
  dp.resize(5);
  for (int j = 0; j < 5; j++) {
    dp[j].resize(5);
    for (int i = 0; i <= j; i++) {
      unsigned int pb = UINT_MAX;
      for (int k = i; k <= j; k++) {
        if (~a[k][i] && ~b[j][k]) {
          pb = min(pb, (unsigned int)(a[k][i] + b[j][k]));
        }
      }
      dp[j][i] = (int)pb;
    }
  }
  return dp;
}
inline void build(vector<vector<vector<int> > >& st, const int& n) {
  for (int i = n - 1; i > 0; i--) st[i] = merge(st[i << 1], st[(i << 1) + 1]);
}
inline int query(int& l, int& r, vector<vector<vector<int> > >& st,
                 const int& n) {
  vector<vector<int> > ansl, ansr;
  for (; l < r; l = l >> 1, r = r >> 1) {
    if (l & 1) {
      ansl = merge(ansl, st[l]);
      ++l;
    }
    if (r & 1) {
      --r;
      ansr = merge(st[r], ansr);
    }
  }
  return merge(ansl, ansr)[4][0];
}
int main() {
  int q, n;
  char s[200010];
  scanf("%d %d %s", &n, &q, s);
  vector<vector<vector<int> > > st(n << 1);
  for (int i = 0; i < n; i++) {
    switch (s[i]) {
      case '2':
        st[n + i] = {
            {1}, {0, 0}, {-1, -1, 0}, {-1, -1, -1, 0}, {-1, -1, -1, -1, 0}};
        break;
      case '0':
        st[n + i] = {
            {0}, {-1, 1}, {-1, 0, 0}, {-1, -1, -1, 0}, {-1, -1, -1, -1, 0}};
        break;
      case '1':
        st[n + i] = {
            {0}, {-1, 0}, {-1, -1, 1}, {-1, -1, 0, 0}, {-1, -1, -1, -1, 0}};
        break;
      case '7':
        st[n + i] = {
            {0}, {-1, 0}, {-1, -1, 0}, {-1, -1, -1, 1}, {-1, -1, -1, 0, 0}};
        break;
      case '6':
        st[n + i] = {
            {0}, {-1, 0}, {-1, -1, 0}, {-1, -1, -1, 1}, {-1, -1, -1, -1, 1}};
        break;
      default:
        st[n + i] = {
            {0}, {-1, 0}, {-1, -1, 0}, {-1, -1, -1, 0}, {-1, -1, -1, -1, 0}};
    }
  }
  build(st, n);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l += n - 1;
    r += n;
    printf("%d\n", query(l, r, st, n));
  }
  return 0;
}
