#include <bits/stdc++.h>
using namespace std;
int getint() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
const int inf = 1e9;
int nxt[] = {2, 0, 1, 7};
int tr[212345 << 2][5][5];
void debug(int a[5][5]) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      if (a[i][j] >= 20)
        cout << "X ";
      else
        cout << a[i][j] << " ";
    }
    cout << endl;
  }
}
int daxiao[5][5];
void init(int a[5][5], int x) {
  memset(a, 0x3f, sizeof(daxiao));
  for (int i = 0; i < 4; ++i) {
    if (x == nxt[i])
      a[i][i + 1] = 0, a[i][i] = 1;
    else
      a[i][i] = 0;
  }
  if (x == 6) {
    a[4][4] = 1;
    a[3][3] = 1;
  } else
    a[4][4] = 0;
  return;
}
void merge(int a[5][5], int b[5][5], int c[5][5]) {
  int z;
  for (int i = 0; i < 5; ++i)
    for (int k = i; k < 5; ++k) {
      z = 1e9;
      for (int j = i; j <= k; ++j) z = min(z, a[i][j] + b[j][k]);
      c[i][k] = z;
    }
  return;
}
char s[212345];
void build(int i, int l, int r) {
  if (l == r) {
    init(tr[i], s[l] - '0');
    return;
  }
  int mid = (l + r) >> 1;
  build((i << 1), l, (mid));
  build((i << 1 | 1), (mid + 1), r);
  merge(tr[(i << 1)], tr[(i << 1 | 1)], tr[i]);
  return;
}
int ans[5][5];
int tmp[5][5];
int fl, fr;
void query(int l, int r, int i) {
  if (fl <= l && r <= fr) {
    if (fl == l) {
      memcpy(ans, tr[i], sizeof(ans));
    } else {
      merge(ans, tr[i], tmp);
      swap(ans, tmp);
    }
    return;
  }
  int mid = (l + r) >> 1;
  if (fr <= (mid))
    query(l, (mid), (i << 1));
  else if (fl >= (mid + 1))
    query((mid + 1), r, (i << 1 | 1));
  else {
    query(l, (mid), (i << 1));
    query((mid + 1), r, (i << 1 | 1));
  }
}
int main() {
  int n = getint(), m = getint();
  scanf("%s", s + 1);
  build(1, 1, n);
  while (m--) {
    fl = getint(), fr = getint();
    query(1, n, 1);
    int x = ans[0][4];
    if (x > n)
      printf("-1\n");
    else
      printf("%d\n", x);
  }
}
