#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
const int N = 1234567;
const int INF = 1e9;
struct Node {
  int a[5][5];
  Node() {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++)
        if (i != j)
          a[i][j] = INF;
        else
          a[i][j] = 0;
  }
  Node(char c) {
    *this = Node();
    if (c == '2') {
      a[0][0] = 1;
      a[0][1] = 0;
    } else if (c == '0') {
      a[1][1] = 1;
      a[1][2] = 0;
    } else if (c == '1') {
      a[2][2] = 1;
      a[2][3] = 0;
    } else if (c == '6') {
      a[3][3] = 1;
      a[4][4] = 1;
    } else if (c == '7') {
      a[3][3] = 1;
      a[3][4] = 0;
    }
  }
};
int n, q;
string s;
Node it[800010];
Node Merge(Node A, Node B) {
  Node C = Node();
  for (int i = 0; i < 5; i++) C.a[i][i] = INF;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++) {
        C.a[i][j] = min(C.a[i][j], A.a[i][k] + B.a[k][j]);
      }
  return C;
}
void build(int node, int l, int r) {
  if (l > r) return;
  if (l == r) {
    it[node] = Node(s[l]);
  } else {
    int mid = (l + r) / 2;
    build(2 * node, l, mid);
    build(2 * node + 1, mid + 1, r);
    it[node] = Merge(it[2 * node], it[2 * node + 1]);
  }
}
Node Get(int node, int l, int r, int L, int R) {
  if (l > R || r < L) return Node();
  if (l >= L && r <= R) {
    return it[node];
  } else {
    int mid = (l + r) / 2;
    Node Lnode = Get(2 * node, l, mid, L, R);
    Node Rnode = Get(2 * node + 1, mid + 1, r, L, R);
    return Merge(Lnode, Rnode);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q >> s;
  s = ' ' + s;
  build(1, 1, n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    Node ans = Get(1, 1, n, l, r);
    if (ans.a[0][4] > (r - l + 1)) {
      cout << -1 << endl;
    } else {
      cout << ans.a[0][4] << endl;
    }
  }
  return 0;
}
