#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
char a[N];
struct cc {
  int dp[5][5];
  cc operator+(const cc &x) const {
    cc y;
    for (int len = 0; len < 5; len++) {
      for (int i = 0; i + len < 5; i++) {
        int l = i, r = i + len;
        y.dp[l][r] = 0x3f3f3f3f;
        for (int k = l; k <= r; k++) {
          y.dp[l][r] = min(y.dp[l][r], dp[l][k] + x.dp[k][r]);
        }
      }
    }
    return y;
  }
} sum[N << 2];
void build(int l, int r, int rt) {
  if (l == r) {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) sum[rt].dp[i][j] = (i == j) ? 0 : 0x3f3f3f3f;
    if (a[l] == '2') {
      sum[rt].dp[0][0] = 1;
      sum[rt].dp[0][1] = 0;
    } else if (a[l] == '0') {
      sum[rt].dp[1][1] = 1;
      sum[rt].dp[1][2] = 0;
    } else if (a[l] == '1') {
      sum[rt].dp[2][2] = 1;
      sum[rt].dp[2][3] = 0;
    } else if (a[l] == '7') {
      sum[rt].dp[3][3] = 1;
      sum[rt].dp[3][4] = 0;
    } else if (a[l] == '6') {
      sum[rt].dp[3][3] = 1;
      sum[rt].dp[4][4] = 1;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
cc query(int L, int R, int l, int r, int rt) {
  if (l >= L && r <= R) {
    return sum[rt];
  }
  int mid = (l + r) >> 1;
  if (R <= mid) return query(L, R, l, mid, rt << 1);
  if (L > mid) return query(L, R, mid + 1, r, rt << 1 | 1);
  return query(L, mid, l, mid, rt << 1) +
         query(mid + 1, R, mid + 1, r, rt << 1 | 1);
}
cc ans;
int main() {
  int n, m, i, j, k, num, q, sum, l, r;
  scanf("%d%d", &n, &q);
  scanf("%s", a + 1);
  build(1, n, 1);
  while (q--) {
    scanf("%d%d", &l, &r);
    num = query(l, r, 1, n, 1).dp[0][4];
    printf("%d\n", num == 0x3f3f3f3f ? -1 : num);
  }
  return 0;
}
