#include <bits/stdc++.h>
using namespace std;
struct st {
  int dp[5][5];
  st(void) {
    for (int i = 0; i <= 4; ++i)
      for (int j = 0; j <= 4; ++j) dp[i][j] = 1e9;
  };
};
st operator+(st a, st b) {
  st c;
  for (int i = 0; i <= 4; ++i)
    for (int k = i; k <= 4; ++k)
      for (int j = k; j <= 4; ++j)
        c.dp[i][j] = min(c.dp[i][j], a.dp[i][k] + b.dp[k][j]);
  return c;
};
st t[1 << 21];
char s[1 << 20];
int v[1 << 20];
void build(int p, int u, int node) {
  if (p == u) {
    for (int i = 0; i <= 4; ++i) t[node].dp[i][i] = 0;
    if (!v[p]) return;
    if (v[p] <= 3)
      t[node].dp[v[p] - 1][v[p] - 1] = 1, t[node].dp[v[p] - 1][v[p]] = 0;
    else if (v[p] == 4)
      t[node].dp[3][4] = 0;
    else
      t[node].dp[3][3] = t[node].dp[4][4] = 1;
  } else {
    int m = (p + u) / 2;
    build(p, m, node << 1);
    build(m + 1, u, node << 1 | 1);
    t[node] = t[node << 1] + t[node << 1 | 1];
  }
}
st query(int p, int u, int l, int r, int node) {
  st ans;
  if (l <= p && u <= r)
    ans = t[node];
  else {
    int m = (p + u) / 2;
    if (l <= m && m + 1 <= r)
      ans = query(p, m, l, r, node << 1) + query(m + 1, u, l, r, node << 1 | 1);
    else if (l <= m)
      ans = query(p, m, l, r, node << 1);
    else
      ans = query(m + 1, u, l, r, node << 1 | 1);
  }
  return ans;
}
int main(void) {
  srand(time(0));
  cout << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n, q;
  cin >> n >> q;
  cin >> (s + 1);
  for (int i = 1; i <= n; ++i)
    if (s[i] == '2')
      v[i] = 1;
    else if (s[i] == '0')
      v[i] = 2;
    else if (s[i] == '1')
      v[i] = 3;
    else if (s[i] == '7')
      v[i] = 4;
    else if (s[i] == '6')
      v[i] = 5;
  build(1, n, 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    auto brut = [&] {
      static int dp[6];
      for (int i = 0; i < 5; ++i) dp[i] = 1e9;
      dp[0] = 0;
      for (int i = l; i <= r; ++i)
        if (v[i]) {
          if (v[i] == 5)
            ++dp[3], ++dp[4];
          else {
            dp[v[i]] = min(dp[v[i]], dp[v[i] - 1]);
            ++dp[v[i] - 1];
          }
        }
      if (dp[4] > 1e7)
        return -1;
      else
        return dp[4];
    };
    int ans = query(1, n, l, r, 1).dp[0][4];
    if (ans > 1e7) ans = -1;
    cout << ans << '\n';
  }
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}
