#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n, q;
char str[MAXN];
struct Matrix {
  int m[5][5];
  void clear() {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) m[i][j] = i == j ? 0 : MAXN;
  }
  Matrix operator+(Matrix b) {
    Matrix ans;
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        ans.m[i][j] = MAXN;
        for (int k = 0; k < 5; k++)
          ans.m[i][j] = min(ans.m[i][j], m[i][k] + b.m[k][j]);
      }
    return ans;
  }
};
struct SegmentTree {
  Matrix tree[MAXN << 2];
  inline int LSON(int x) { return x << 1; }
  inline int RSON(int x) { return x << 1 | 1; }
  void pushUp(int root) { tree[root] = tree[LSON(root)] + tree[RSON(root)]; }
  void build(int root, int l, int r) {
    if (l == r) {
      tree[root].clear();
      if (str[l] == '2') tree[root].m[0][0] = 1, tree[root].m[0][1] = 0;
      if (str[l] == '0') tree[root].m[1][1] = 1, tree[root].m[1][2] = 0;
      if (str[l] == '1') tree[root].m[2][2] = 1, tree[root].m[2][3] = 0;
      if (str[l] == '6') tree[root].m[3][3] = tree[root].m[4][4] = 1;
      if (str[l] == '7') tree[root].m[3][4] = 0, tree[root].m[3][3] = 1;
      return;
    }
    int mid = (l + r) >> 1;
    build(LSON(root), l, mid);
    build(RSON(root), mid + 1, r);
    pushUp(root);
  }
  Matrix query(int root, int l, int r, int nl, int nr) {
    if (l >= nl && r <= nr) return tree[root];
    int mid = (l + r) >> 1;
    Matrix ans;
    ans.clear();
    if (nl <= mid) ans = ans + query(LSON(root), l, mid, nl, nr);
    if (nr > mid) ans = ans + query(RSON(root), mid + 1, r, nl, nr);
    return ans;
  }
} st;
int main() {
  scanf("%d%d", &n, &q);
  scanf("%s", str + 1);
  st.build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    int res = st.query(1, 1, n, l, r).m[0][4];
    if (res == MAXN) res = -1;
    printf("%d\n", res);
  }
  return 0;
}
