#include <bits/stdc++.h>
using namespace std;
char str[200001];
struct Node {
  int prefix[5][5];
  Node() {
    memset(prefix, 0x3f, sizeof(prefix));
    for (int i = 0; i < 5; i++) prefix[i][i] = 0;
  }
  Node(int idx, char ch) {
    memset(prefix, 0x3f, sizeof(prefix));
    for (int i = 0; i < 5; i++) prefix[i][i] = 0;
    if (ch == '2') {
      prefix[0][0] = 1;
      prefix[0][1] = 0;
    } else if (ch == '0') {
      prefix[1][1] = 1;
      prefix[1][2] = 0;
    } else if (ch == '1') {
      prefix[2][2] = 1;
      prefix[2][3] = 0;
    } else if (ch == '7') {
      prefix[3][3] = 1;
      prefix[3][4] = 0;
    } else if (ch == '6') {
      prefix[3][3] = 1;
      prefix[4][4] = 1;
    }
  }
  Node junta(Node& o) {
    Node res;
    memset(res.prefix, 0x3f, sizeof(res.prefix));
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          res.prefix[i][k] =
              min(res.prefix[i][k], prefix[i][j] + o.prefix[j][k]);
    return res;
  }
};
struct Segment_Tree {
  int size;
  vector<Node> segt;
  Segment_Tree(int n, char* arr) {
    size = n;
    segt.resize(4 * n + 1);
    build(arr);
  }
  int left(int node) { return 2 * node; }
  int right(int node) { return 2 * node + 1; }
  void build(char* arr, int node, int inicio, int fin) {
    if (inicio == fin) {
      segt[node] = Node(inicio, arr[inicio]);
      return;
    }
    build(arr, left(node), inicio, (inicio + fin) / 2);
    build(arr, right(node), (inicio + fin) / 2 + 1, fin);
    segt[node] = segt[left(node)].junta(segt[right(node)]);
  }
  Node num(int node, int inicio, int fin, int i, int j) {
    if (j < inicio || i > fin) return Node();
    if (inicio >= i && fin <= j) return segt[node];
    Node p1 = num(left(node), inicio, (inicio + fin) / 2, i, j);
    Node p2 = num(right(node), (inicio + fin) / 2 + 1, fin, i, j);
    return p1.junta(p2);
  }
  void build(char* arr) { build(arr, 1, 0, size - 1); }
  Node num(int i, int j) { return num(1, 0, size - 1, i, j); }
};
int main() {
  int n, q;
  cin >> n >> q;
  cin >> str;
  Segment_Tree tree(n, str);
  while (q--) {
    int a, b;
    cin >> a >> b;
    Node res = tree.num(a - 1, b - 1);
    if (res.prefix[0][4] != 0x3f3f3f3f)
      cout << res.prefix[0][4] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
