#include <bits/stdc++.h>
using namespace std;
char t[200002];
struct node {
  int nt[5][5];
};
node st[4 * 200000 + 2];
node merge(node a, node b) {
  if (a.nt[0][0] == -1) return b;
  if (b.nt[0][0] == -1) return a;
  node ans;
  for (int i = 0; i <= 4; i++)
    for (int j = 0; j <= 4; j++) ans.nt[i][j] = -1e9;
  for (int i = 0; i <= 4; i++) {
    for (int j = i; j <= 4; j++) {
      ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][j]);
      ans.nt[i][j] = max(ans.nt[i][j], b.nt[i][j]);
      for (int k = i; k <= j; k++) {
        ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][k] + b.nt[k][j]);
      }
    }
  }
  return ans;
}
void build(int n, int s, int e) {
  if (s == e) {
    for (int i = 0; i <= 4; i++)
      for (int j = 0; j <= 4; j++) st[n].nt[i][j] = -1e9;
    for (int i = 0; i <= 4; i++) st[n].nt[i][i] = 1;
    if (t[s] == '2') {
      st[n].nt[0][1] = 1;
      st[n].nt[0][0] = -1e9;
    } else if (t[s] == '0') {
      st[n].nt[1][2] = 1;
      st[n].nt[1][1] = -1e9;
    } else if (t[s] == '1') {
      st[n].nt[2][3] = 1;
      st[n].nt[2][2] = -1e9;
    } else if (t[s] == '7') {
      st[n].nt[3][4] = 1;
      st[n].nt[3][3] = -1e9;
    } else if (t[s] == '6') {
      st[n].nt[3][3] = -1e9;
      st[n].nt[4][4] = -1e9;
    }
  } else {
    int mid = (s + e) / 2;
    build(n + n, s, mid);
    build(n + n + 1, mid + 1, e);
    st[n] = merge(st[n + n], st[n + n + 1]);
  }
}
node query(int n, int s, int e, int l, int r) {
  if (s > r || l > e) {
    node ans;
    ans.nt[0][0] = -1;
    return ans;
  }
  if (l <= s && e <= r) return st[n];
  int mid = (s + e) / 2;
  return merge(query(n + n, s, mid, l, r), query(n + n + 1, mid + 1, e, l, r));
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  scanf("%s", t + 1);
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    node ans = query(1, 1, n, l, r);
    if (ans.nt[0][4] < 0)
      printf("-1\n");
    else
      printf("%d\n", r - l + 1 - ans.nt[0][4]);
  }
}
