#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    sum = sum * 10 + (c ^ 48);
    c = getchar();
  }
  return sum;
}
void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + 48);
}
inline void writeln(int x) {
  write(x);
  putchar('\n');
}
int const maxn = 200005;
int const inf = 1e8;
int n, m, a[maxn];
struct matrix {
  int a[6][6];
  inline void init() {
    for (int i = 1; i <= 5; i++)
      for (int j = 1; j <= 5; j++) a[i][j] = inf;
    for (int i = 1; i <= 5; i++) a[i][i] = 0;
  }
} sum[maxn << 2];
inline matrix operator*(matrix a, matrix b) {
  matrix c;
  for (int i = 1; i <= 5; i++)
    for (int j = 1; j <= 5; j++) c.a[i][j] = inf;
  for (int k = 1; k <= 5; k++)
    for (int i = 1; i <= 5; i++)
      for (int j = 1; j <= 5; j++)
        c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]);
  return c;
}
inline matrix gbm(int num) {
  matrix res;
  res.init();
  if (num == 2) res.a[1][1] = 1, res.a[2][1] = 0;
  if (num == 0) res.a[2][2] = 1, res.a[3][2] = 0;
  if (num == 1) res.a[3][3] = 1, res.a[4][3] = 0;
  if (num == 7) res.a[4][4] = 1, res.a[5][4] = 0;
  if (num == 6) res.a[4][4] = 1, res.a[5][5] = 1;
  return res;
}
void build(int k, int l, int r) {
  if (l == r) {
    sum[k] = gbm(a[l]);
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  sum[k] = sum[k << 1 | 1] * sum[k << 1];
}
matrix query(int k, int l, int r, int x, int y) {
  if (l >= x && r <= y) return sum[k];
  int mid = (l + r) >> 1;
  if (y <= mid) return query(k << 1, l, mid, x, y);
  if (x > mid) return query(k << 1 | 1, mid + 1, r, x, y);
  return query(k << 1 | 1, mid + 1, r, x, y) * query(k << 1, l, mid, x, y);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) scanf("%1d", &a[i]);
  build(1, 1, n);
  matrix start;
  start.a[1][1] = 0;
  for (int i = 2; i <= 5; i++) start.a[i][1] = inf;
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    matrix res = query(1, 1, n, x, y) * start;
    if (res.a[5][1] > y - x + 1)
      writeln(-1);
    else
      writeln(res.a[5][1]);
  }
  return 0;
}
