#include <bits/stdc++.h>
using namespace std;
struct debug_t {
  template <typename T>
  debug_t& operator<<(const T& value) {
    return *this;
  }
  debug_t& write(const char data[], streamsize n) { return *this; }
} debug;
enum {
  I00,
  I01,
  I02,
  I03,
  I04,
  I11,
  I12,
  I13,
  I14,
  I22,
  I23,
  I24,
  I33,
  I34,
  I44,
};
struct Matrix {
  int mx[15];
  int& operator[](int i) { return mx[i]; }
  int operator[](int i) const { return mx[i]; }
  Matrix& operator|=(const Matrix& rhs) {
    mx[I44] += rhs[I44];
    mx[I34] = min(0x3F3F3F3F, min(mx[I33] + rhs[I34], mx[I34] + rhs[I44]));
    mx[I33] += rhs[I33];
    mx[I24] = min(0x3F3F3F3F, min(mx[I22] + rhs[I24],
                                  min(mx[I23] + rhs[I34], mx[I24] + rhs[I44])));
    mx[I23] = min(0x3F3F3F3F, min(mx[I22] + rhs[I23], mx[I23] + rhs[I33]));
    mx[I22] += rhs[I22];
    mx[I14] =
        min(0x3F3F3F3F, min(mx[I11] + rhs[I14],
                            min(mx[I12] + rhs[I24],
                                min(mx[I13] + rhs[I34], mx[I14] + rhs[I44]))));
    mx[I13] = min(0x3F3F3F3F, min(mx[I11] + rhs[I13],
                                  min(mx[I12] + rhs[I23], mx[I13] + rhs[I33])));
    mx[I12] = min(0x3F3F3F3F, min(mx[I11] + rhs[I12], mx[I12] + rhs[I22]));
    mx[I11] += rhs[I11];
    mx[I04] = min(0x3F3F3F3F,
                  min(mx[I00] + rhs[I04],
                      min(mx[I01] + rhs[I14],
                          min(mx[I02] + rhs[I24],
                              min(mx[I03] + rhs[I34], mx[I04] + rhs[I44])))));
    mx[I03] =
        min(0x3F3F3F3F, min(mx[I00] + rhs[I03],
                            min(mx[I01] + rhs[I13],
                                min(mx[I02] + rhs[I23], mx[I03] + rhs[I33]))));
    mx[I02] = min(0x3F3F3F3F, min(mx[I00] + rhs[I02],
                                  min(mx[I01] + rhs[I12], mx[I02] + rhs[I22])));
    mx[I01] = min(0x3F3F3F3F, min(mx[I00] + rhs[I01], mx[I01] + rhs[I11]));
    mx[I00] += rhs[I00];
    return *this;
  }
};
Matrix mxdef;
template <int N>
struct SegTree {
  int n;
  Matrix t[N << 1];
  void init() {
    int size = n;
    while (--size) {
      t[size] = t[size << 1];
      t[size] |= t[size << 1 | 0x1];
    }
  }
  void getMin(int l, int r, Matrix& result) const {
    Matrix temp[2] = {mxdef};
    bool cur = 0;
    result = mxdef;
    for (l += n, r += n; l != r; l >>= 1, r >>= 1) {
      if (l & 0x1) result |= t[l++];
      if (r & 0x1) {
        temp[!cur] = t[--r];
        temp[!cur] |= temp[cur];
        cur = !cur;
      }
    }
    result |= temp[cur];
  }
};
SegTree<(int)2e5> sgt;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Matrix mx2, mx0, mx1, mx7, mx6;
  memset(mx2.mx, 0x3F, sizeof mx2.mx);
  memset(mx0.mx, 0x3F, sizeof mx0.mx);
  memset(mx1.mx, 0x3F, sizeof mx1.mx);
  memset(mx7.mx, 0x3F, sizeof mx7.mx);
  memset(mx6.mx, 0x3F, sizeof mx6.mx);
  memset(mxdef.mx, 0x3F, sizeof mxdef.mx);
  mx2[I00] = 1;
  mx2[I01] = 0;
  mx2[I11] = 0;
  mx2[I22] = 0;
  mx2[I33] = 0;
  mx2[I44] = 0;
  mx0[I00] = 0;
  mx0[I11] = 1;
  mx0[I12] = 0;
  mx0[I22] = 0;
  mx0[I33] = 0;
  mx0[I44] = 0;
  mx1[I00] = 0;
  mx1[I11] = 0;
  mx1[I22] = 1;
  mx1[I23] = 0;
  mx1[I33] = 0;
  mx1[I44] = 0;
  mx7[I00] = 0;
  mx7[I11] = 0;
  mx7[I22] = 0;
  mx7[I33] = 1;
  mx7[I34] = 0;
  mx7[I44] = 0;
  mx6[I00] = 0;
  mx6[I11] = 0;
  mx6[I22] = 0;
  mx6[I33] = 1;
  mx6[I44] = 1;
  mxdef[I00] = 0;
  mxdef[I11] = 0;
  mxdef[I22] = 0;
  mxdef[I33] = 0;
  mxdef[I44] = 0;
  int q;
  while (cin >> sgt.n >> q) {
    for (int i = 0; i < sgt.n; i++) {
      char c;
      cin >> c;
      switch (c) {
        case '0':
          sgt.t[i + sgt.n] = mx0;
          break;
        case '1':
          sgt.t[i + sgt.n] = mx1;
          break;
        case '2':
          sgt.t[i + sgt.n] = mx2;
          break;
        case '6':
          sgt.t[i + sgt.n] = mx6;
          break;
        case '7':
          sgt.t[i + sgt.n] = mx7;
          break;
        default:
          sgt.t[i + sgt.n] = mxdef;
      }
    }
    sgt.init();
    Matrix mx;
    while (q--) {
      int left, right;
      cin >> left >> right;
      left--;
      sgt.getMin(left, right, mx);
      if (mx[I04] > right - left)
        cout << "-1\n";
      else
        cout << mx[I04] << '\n';
    }
    debug << '\n';
  }
}
