#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
struct rec {
  int dp[5][5];
  void init() {
    for (int i = 0; i < 5; ++i)
      for (int j = i; j < 5; ++j)
        if (i == j)
          dp[i][j] = 0;
        else
          dp[i][j] = inf;
  }
  rec operator+(const rec &a) const {
    rec b;
    for (int i = 0; i < 5; ++i)
      for (int j = i; j < 5; ++j) {
        b.dp[i][j] = inf;
        for (int k = i; k <= j; ++k)
          b.dp[i][j] = min(b.dp[i][j], dp[i][k] + a.dp[k][j]);
      }
    return b;
  }
} seg[200010 * 3], ans;
char st[200010];
int n, m;
void build(int x, int s, int t) {
  if (s == t) {
    seg[x].init();
    int i = s - 1;
    if (st[i] == '2')
      seg[x].dp[0][0] = 1, seg[x].dp[0][1] = 0;
    else if (st[i] == '0')
      seg[x].dp[1][1] = 1, seg[x].dp[1][2] = 0;
    else if (st[i] == '1')
      seg[x].dp[2][2] = 1, seg[x].dp[2][3] = 0;
    else if (st[i] == '7')
      seg[x].dp[3][3] = 1, seg[x].dp[3][4] = 0;
    else if (st[i] == '6')
      seg[x].dp[3][3] = 1, seg[x].dp[4][4] = 1;
    return;
  }
  int mid = s + t >> 1;
  build(x << 1, s, mid);
  build(x << 1 | 1, mid + 1, t);
  seg[x] = seg[x << 1] + seg[x << 1 | 1];
}
void query(int x, int s, int t, int l, int r) {
  if (l <= s && t <= r) {
    ans = ans + seg[x];
    return;
  }
  int mid = s + t >> 1;
  if (l <= mid) query(x << 1, s, mid, l, r);
  if (mid < r) query(x << 1 | 1, mid + 1, t, l, r);
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", st);
  build(1, 1, n);
  int l, r;
  while (m--) {
    scanf("%d%d", &l, &r);
    ans.init();
    query(1, 1, n, l, r);
    if (ans.dp[0][4] == inf)
      printf("-1\n");
    else
      printf("%d\n", ans.dp[0][4]);
  }
  return 0;
}
