#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int N, Q;
char a[200010];
struct Matrix {
  int a[5][5];
  Matrix() {
    for (int i = 0; i <= 4; i++)
      for (int j = 0; j <= 4; j++) a[i][j] = (int)(1e9);
  }
  inline void init() {
    for (int i = 0; i <= 4; i++) a[i][i] = 0;
  }
  inline void print() {
    puts("========");
    for (int i = 0; i <= 4; i++, puts(""))
      for (int j = 0; j <= 4; j++)
        printf("%d  ", a[i][j] >= (int)(1e9) ? -1 : a[i][j]);
    puts("========");
  }
};
Matrix operator+(Matrix x, Matrix y) {
  Matrix re;
  for (int k = 0; k <= 4; k++)
    for (int i = 0; i <= 4; i++)
      if (x.a[i][k] != (int)(1e9))
        for (int j = 0; j <= 4; j++)
          if (y.a[k][j] != (int)(1e9))
            re.a[i][j] = min(re.a[i][j], x.a[i][k] + y.a[k][j]);
  return re;
}
namespace SegmentTree {
Matrix tree[200010 << 2];
inline void Build(int now, int l, int r) {
  if (l == r) {
    tree[now].init();
    switch (a[l]) {
      case '2':
        tree[now].a[0][0] = 1, tree[now].a[0][1] = 0;
        break;
      case '0':
        tree[now].a[1][1] = 1, tree[now].a[1][2] = 0;
        break;
      case '1':
        tree[now].a[2][2] = 1, tree[now].a[2][3] = 0;
        break;
      case '7':
        tree[now].a[3][3] = 1, tree[now].a[3][4] = 0;
        break;
      case '6':
        tree[now].a[3][3] = 1, tree[now].a[4][4] = 1;
        break;
    }
    return;
  }
  int mid = (l + r) >> 1;
  Build(now << 1, l, mid);
  Build(now << 1 | 1, mid + 1, r);
  tree[now] = tree[now << 1] + tree[now << 1 | 1];
}
inline Matrix Query(int now, int l, int r, int L, int R) {
  if (L == l && R == r) return tree[now];
  int mid = (l + r) >> 1;
  if (R <= mid)
    return Query(now << 1, l, mid, L, R);
  else if (L > mid)
    return Query(now << 1 | 1, mid + 1, r, L, R);
  else
    return Query(now << 1, l, mid, L, mid) +
           Query(now << 1 | 1, mid + 1, r, mid + 1, R);
}
}  // namespace SegmentTree
using namespace SegmentTree;
int main() {
  N = read(), Q = read();
  scanf("%s", a + 1);
  SegmentTree::Build(1, 1, N);
  while (Q--) {
    int l = read(), r = read();
    Matrix ans = SegmentTree::Query(1, 1, N, l, r);
    printf("%d\n", ans.a[0][4] >= (int)(1e9) ? -1 : ans.a[0][4]);
  }
  return 0;
}
