#include <bits/stdc++.h>
using namespace std;
const double eps = 0.000000000000001;
string convertstring(long long n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
char s[200005];
struct node {
  int a[5][5];
  void merge(const node b, const node d) {
    int c[5][5];
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        c[i][j] = 999999999;
        for (int k = 0; k < 5; ++k) {
          c[i][j] = min(c[i][j], b.a[i][k] + d.a[k][j]);
        }
      }
    }
    for (int i = 0; i < 5; ++i)
      for (int j = 0; j < 5; ++j) a[i][j] = c[i][j];
  }
};
node tree[200005 * 4];
void init(int a[5][5], int x) {
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < i; ++j) a[i][j] = 999999999;
  a[0][0] = (x == 2 ? 1 : 0);
  a[0][2] = a[0][3] = a[0][4] = 999999999;
  a[1][1] = (x == 0 ? 1 : 0);
  a[1][3] = a[1][4] = 999999999;
  a[2][2] = (x == 1 ? 1 : 0);
  a[2][4] = 999999999;
  a[3][3] = (((x == 7) || (x == 6)) ? 1 : 0);
  a[4][4] = (x == 6 ? 1 : 0);
  a[0][1] = (x == 2 ? 0 : 999999999);
  a[1][2] = (x == 0 ? 0 : 999999999);
  a[2][3] = (x == 1 ? 0 : 999999999);
  a[3][4] = (x == 7 ? 0 : 999999999);
}
void build(int nd, int st, int en) {
  if (st == en) {
    init(tree[nd].a, s[st] - '0');
    return;
  }
  int mid = st + en >> 1;
  build(nd << 1, st, mid);
  build((nd << 1) + 1, mid + 1, en);
  tree[nd].merge(tree[nd << 1], tree[(nd << 1) + 1]);
}
node bekar;
node query(int nd, int st, int en, int l, int r) {
  if (st > en || st > r || en < l) return bekar;
  if (st >= l && en <= r) return tree[nd];
  node x;
  x.merge(query(nd << 1, st, (st + en >> 1), l, r),
          query((nd << 1) + 1, (st + en >> 1) + 1, en, l, r));
  return x;
}
int main() {
  int n, q, i, j;
  scanf("%d%d", &n, &q);
  ;
  scanf("%s", s + 1);
  for (i = 0; i < 5; ++i)
    for (j = 0; j < 5; ++j) bekar.a[i][j] = ((i != j) ? 999999999 : 0);
  build(1, 1, n);
  node x = tree[1];
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    ;
    node x = query(1, 1, n, l, r);
    printf("%d\n", (x.a[0][4] >= 999999999) ? -1 : x.a[0][4]);
  }
  return 0;
}
