#include <bits/stdc++.h>
using namespace std;
array<array<int, 5>, 5> identidade() {
  array<array<int, 5>, 5> arr;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++) arr[i][j] = 100000000, arr[i][i] = 0;
  return arr;
}
array<array<int, 5>, 5> get(char c) {
  array<array<int, 5>, 5> arr = identidade();
  if (c == '2') arr[0][0] = 1, arr[0][1] = 0;
  if (c == '0') arr[1][1] = 1, arr[1][2] = 0;
  if (c == '1') arr[2][2] = 1, arr[2][3] = 0;
  if (c == '7') arr[3][3] = 1, arr[3][4] = 0;
  if (c == '6') arr[4][4] = 1, arr[3][3] = 1;
  return arr;
}
array<array<int, 5>, 5> merge(array<array<int, 5>, 5> a,
                              array<array<int, 5>, 5> b) {
  array<array<int, 5>, 5> arr;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++) arr[i][j] = 100000000;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++)
        arr[i][j] = min({arr[i][j], a[i][k] + b[k][j], 100000000});
  return arr;
}
array<array<int, 5>, 5> tree[4 * 200005];
char s[200005];
void build(int node, int a, int b) {
  if (a == b) {
    tree[node] = get(s[a]);
  } else {
    build(node * 2, a, (a + b) / 2);
    build(node * 2 + 1, (a + b) / 2 + 1, b);
    tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
  }
}
array<array<int, 5>, 5> query(int node, int a, int b, int i, int j) {
  if (j < a || i > b) return identidade();
  if (a >= i && b <= j) return tree[node];
  array<array<int, 5>, 5> l = query(node * 2, a, (a + b) / 2, i, j);
  array<array<int, 5>, 5> r = query(node * 2 + 1, (a + b) / 2 + 1, b, i, j);
  return merge(l, r);
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", s);
  build(1, 0, n - 1);
  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--, b--;
    array<array<int, 5>, 5> node = query(1, 0, n - 1, a, b);
    printf("%d\n", node[0][4] == 100000000 ? -1 : node[0][4]);
  }
  return 0;
}
