#include <bits/stdc++.h>
using namespace std;
const int MN = 200005, P2 = (1 << 18), MTS = (1 << 19) + 5, inf = 1000000005;
char opis[MN];
int co[] = {2, 3, 1, 0, 0, 0, 0, 4, 0, 0};
struct Node {
  int dp[5][5];
};
Node t[MTS], res;
inline void czysc(Node& N) {
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 5; ++j) N.dp[i][j] = inf;
  for (int i = 0; i < 5; ++i) N.dp[i][i] = 0;
}
inline void fiks(Node& R, Node& A, Node& B) {
  Node tmp;
  czysc(tmp);
  for (int i = 0; i < 5; ++i) tmp.dp[i][i] = inf;
  for (int i = 0; i < 5; ++i)
    for (int j = i; j < 5; ++j)
      for (int k = i; k <= j; ++k)
        tmp.dp[i][j] = min(tmp.dp[i][j], A.dp[i][k] + B.dp[k][j]);
  for (int i = 0; i < 5; ++i)
    for (int j = i; j < 5; ++j) R.dp[i][j] = tmp.dp[i][j];
}
void licz(int cur, int a, int b, int p, int k) {
  if (a == p && b == k) {
    fiks(res, res, t[cur]);
    return;
  }
  int mid = (p + k) >> 1;
  if (a <= mid) licz((cur << 1), a, min(b, mid), p, mid);
  if (b > mid) licz(1 + (cur << 1), max(a, mid + 1), b, mid + 1, k);
}
inline int wynik(int a, int b) {
  czysc(res);
  licz(1, a, b, 0, P2 - 1);
  if (res.dp[0][4] == inf) return -1;
  return res.dp[0][4];
}
inline void wstaw(int num, int pos) {
  pos += P2;
  if (num == 6) {
    t[pos].dp[3][3] = 1;
    t[pos].dp[4][4] = 1;
  }
  if (co[num]) {
    t[pos].dp[co[num] - 1][co[num] - 1] = 1;
    t[pos].dp[co[num] - 1][co[num]] = 0;
  }
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  scanf("%s", opis);
  for (int i = 0; i < MTS; ++i) czysc(t[i]);
  for (int i = 1; i <= n; ++i) wstaw(opis[i - 1] - '0', i);
  for (int i = P2 - 1; i > 0; --i) fiks(t[i], t[(i << 1)], t[1 + (i << 1)]);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", wynik(a, b));
  }
}
