#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct node {
  int dp[5][5];
} ans;
struct ST {
  int l, r;
  node w;
} t[N << 2];
int n, T, l, r;
char s[N];
void update(node &a, node b, node c) {
  int i, j, k;
  memset(a.dp, 0x3f, sizeof(a.dp));
  for (i = 0; i <= 4; i++)
    for (j = 0; j <= 4; j++)
      for (k = 0; k <= 4; k++)
        a.dp[i][j] = min(a.dp[i][j], b.dp[i][k] + c.dp[k][j]);
}
void build(int k, int l, int r) {
  int i;
  t[k].l = l;
  t[k].r = r;
  memset(t[k].w.dp, 0x3f, sizeof(t[k].w.dp));
  if (l == r) {
    if (s[l] == '6') {
      t[k].w.dp[3][3] = 1;
      t[k].w.dp[4][4] = 1;
      for (i = 0; i <= 2; i++) t[k].w.dp[i][i] = 0;
      return;
    }
    for (i = 0; i <= 4; i++) t[k].w.dp[i][i] = 0;
    if (s[l] == '2') {
      t[k].w.dp[0][1] = 0;
      t[k].w.dp[0][0] = 1;
      return;
    }
    if (s[l] == '0') {
      t[k].w.dp[1][2] = 0;
      t[k].w.dp[1][1] = 1;
      return;
    }
    if (s[l] == '1') {
      t[k].w.dp[2][3] = 0;
      t[k].w.dp[2][2] = 1;
      return;
    }
    if (s[l] == '7') {
      t[k].w.dp[3][4] = 0;
      t[k].w.dp[3][3] = 1;
      return;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  update(t[k].w, t[k << 1].w, t[k << 1 | 1].w);
}
void query(int k, int L, int R) {
  if (L <= t[k].l && t[k].r <= R) {
    update(ans, ans, t[k].w);
    return;
  }
  int mid = (t[k].l + t[k].r) >> 1;
  if (L <= mid) query(k << 1, L, R);
  if (R > mid) query(k << 1 | 1, L, R);
}
int main() {
  scanf("%d%d", &n, &T);
  scanf("%s", s + 1);
  build(1, 1, n);
  while (T--) {
    scanf("%d%d", &l, &r);
    memset(ans.dp, 0x3f, sizeof(ans.dp));
    ans.dp[0][0] = 0;
    query(1, l, r);
    if (ans.dp[0][4] <= n)
      printf("%d\n", ans.dp[0][4]);
    else
      printf("-1\n");
  }
  return 0;
}
