#include <bits/stdc++.h>
using namespace std;
struct Matrix {
  int a[6][6];
  int* operator[](int x) { return a[x]; }
} m[800005];
int n, q, a[200005];
Matrix init(int x) {
  Matrix ans;
  for (int i = 1; i <= 5; i++)
    for (int j = 1; j <= 5; j++) ans[i][j] = 1000000000;
  for (int i = 1; i <= 5; i++) ans[i][i] = 0;
  if (x == 0) ans[2][2] = 1, ans[3][2] = 0;
  if (x == 1) ans[3][3] = 1, ans[4][3] = 0;
  if (x == 2) ans[1][1] = 1, ans[2][1] = 0;
  if (x == 6) ans[5][5] = 1, ans[4][4] = 1;
  if (x == 7) ans[4][4] = 1, ans[5][4] = 0;
  return ans;
}
Matrix operator*(Matrix x, Matrix y) {
  Matrix ans;
  for (int i = 1; i <= 5; i++)
    for (int j = 1; j <= 5; j++) ans[i][j] = 1000000000;
  for (int i = 1; i <= 5; i++)
    for (int j = 1; j <= 5; j++)
      for (int k = 1; k <= 5; k++)
        ans[i][j] = min(ans[i][j], x[i][k] + y[k][j]);
  return ans;
}
void build(int k, int l, int r) {
  if (l == r) {
    m[k] = init(a[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(2 * k, l, mid);
  build(2 * k + 1, mid + 1, r);
  m[k] = m[2 * k + 1] * m[2 * k];
  return;
}
Matrix query(int k, int l, int r, int ql, int qr) {
  if (ql <= l && qr >= r) {
    return m[k];
  }
  int mid = (l + r) / 2;
  if (ql <= mid && qr > mid)
    return query(2 * k + 1, mid + 1, r, ql, qr) * query(2 * k, l, mid, ql, qr);
  if (ql <= mid) return query(2 * k, l, mid, ql, qr);
  if (qr > mid) return query(2 * k + 1, mid + 1, r, ql, qr);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    char tmp;
    scanf(" %c", &tmp);
    a[i] = tmp - '0';
  }
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    Matrix tmp = query(1, 1, n, l, r);
    if (tmp[5][1] > r - l + 1)
      printf("-1\n");
    else
      printf("%d\n", tmp[5][1]);
  }
}
