#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  register T sum = 0;
  char cc = getchar();
  int sym = 1;
  while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar();
  if (cc == '-') sym = -1, cc = getchar();
  sum = sum * 10 + cc - '0';
  cc = getchar();
  while (cc >= '0' && cc <= '9') sum = sum * 10 + cc - '0', cc = getchar();
  return sym * sum;
}
template <typename T>
inline T read(T &a) {
  a = read<T>();
  return a;
}
struct Node {
  int a[5][5];
  Node() {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) a[i][j] = i == j ? 0 : 0x7ffffff;
  }
  Node operator+(const Node &b) const {
    Node c;
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        c.a[i][j] = 0x7ffffff;
        for (int k = 0; k < 5; k++)
          c.a[i][j] = min(c.a[i][j], a[i][k] + b.a[k][j]);
      }
    return c;
  }
} node[200010 << 2];
int n, m;
char s[200010];
void build(int k, int l, int r) {
  if (l == r) {
    if (s[l] == '2') {
      node[k].a[0][0] = 1;
      node[k].a[0][1] = 0;
    }
    if (s[l] == '0') {
      node[k].a[1][1] = 1;
      node[k].a[1][2] = 0;
    }
    if (s[l] == '1') {
      node[k].a[2][2] = 1;
      node[k].a[2][3] = 0;
    }
    if (s[l] == '7') {
      node[k].a[3][3] = 1;
      node[k].a[3][4] = 0;
    }
    if (s[l] == '6') {
      node[k].a[3][3] = 1;
      node[k].a[4][4] = 1;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  node[k] = node[k << 1] + node[k << 1 | 1];
}
Node query(int k, int l, int r, int x, int y) {
  if (l >= x && r <= y) return node[k];
  int mid = (l + r) >> 1;
  Node ans;
  if (x <= mid) ans = ans + query(k << 1, l, mid, x, y);
  if (y > mid) ans = ans + query(k << 1 | 1, mid + 1, r, x, y);
  return ans;
}
int main() {
  n = read<int>(), m = read<int>();
  scanf("%s", s + 1);
  build(1, 1, n);
  for (int tot = 1; tot <= m; tot++) {
    int l = read<int>(), r = read<int>();
    int ans = query(1, 1, n, l, r).a[0][4];
    printf("%d\n", ans == 0x7ffffff ? -1 : ans);
  }
  return 0;
}
