#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n, q;
char s[N];
struct Node {
  int m[5][5];
  void clear() { memset(m, 0x3f, sizeof(m)); }
} tr[N * 2];
void push_up(int l, int r) {
  tr[(l + r) | (l != r)].clear();
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++)
        tr[(l + r) | (l != r)].m[i][j] =
            min(tr[(l + r) | (l != r)].m[i][j],
                tr[(l + (l + r) / 2) | (l != (l + r) / 2)].m[i][k] +
                    tr[((l + r) / 2 + 1 + r) | ((l + r) / 2 + 1 != r)].m[k][j]);
}
void build(int l, int r) {
  if (l == r) {
    tr[(l + r) | (l != r)].clear();
    for (int i = 0; i < 5; ++i) tr[(l + r) | (l != r)].m[i][i] = 0;
    if (s[l] == '2')
      tr[(l + r) | (l != r)].m[0][1] = 0, tr[(l + r) | (l != r)].m[0][0] = 1;
    if (s[l] == '0')
      tr[(l + r) | (l != r)].m[1][2] = 0, tr[(l + r) | (l != r)].m[1][1] = 1;
    if (s[l] == '1')
      tr[(l + r) | (l != r)].m[2][3] = 0, tr[(l + r) | (l != r)].m[2][2] = 1;
    if (s[l] == '6')
      tr[(l + r) | (l != r)].m[3][3] = 1, tr[(l + r) | (l != r)].m[4][4] = 1;
    if (s[l] == '7')
      tr[(l + r) | (l != r)].m[3][4] = 0, tr[(l + r) | (l != r)].m[3][3] = 1;
    return;
  }
  build(l, (l + r) / 2);
  build((l + r) / 2 + 1, r);
  push_up(l, r);
}
Node query(int L, int R, int l, int r) {
  if (L <= l && r <= R) return tr[(l + r) | (l != r)];
  if (R <= (l + r) / 2)
    return query(L, R, l, (l + r) / 2);
  else if (L > (l + r) / 2)
    return query(L, R, (l + r) / 2 + 1, r);
  else {
    Node ql = query(L, R, l, (l + r) / 2);
    Node qr = query(L, R, (l + r) / 2 + 1, r);
    Node ret;
    ret.clear();
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          ret.m[i][j] = min(ret.m[i][j], ql.m[i][k] + qr.m[k][j]);
    return ret;
  }
}
int main() {
  scanf("%d%d%s", &n, &q, s + 1);
  build(1, n);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ans = query(l, r, 1, n).m[0][4];
    if (ans == INF) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
