#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
int a, b, c, d, n, m, k, q;
int tr[(1 << 19) + 2][3][3][2];
int mem[(1 << 19) + 2][3][3][2], iter[(1 << 19) + 2][3][3][2];
int T = 0;
char str[200002];
int cs[10][200002];
const char *bad = "2016";
inline int sum(int dig, int l, int r) {
  int ans = cs[dig][r];
  if (l) ans -= cs[dig][l - 1];
  return ans;
}
int fnd(int l, int r, int v, int p0, int p1, bool s7) {
  if (p1 < 2) s7 = 1;
  int &ans = tr[v][p0][p1][s7];
  if (ans <= 1000000000) return ans;
  if (p1 - p0 + 1 > r - l) return ans = 1000000000;
  if (l == r - 1) {
    if (str[l] == bad[p0] && (p1 != 2 || s7))
      return ans = 0;
    else
      return ans = 1000000000;
  }
  int xx = (r + l) / 2;
  ans = sum(bad[p0] - '0', l, xx - 1) + fnd(xx, r, v * 2 + 1, p0, p1, s7);
  for (int _n(p1 - 1), i(p0); i <= _n; i++) {
    int a1 = fnd(l, xx, v * 2, p0, i, s7);
    int a2 = fnd(xx, r, v * 2 + 1, i + 1, p1, s7);
    ans = min(ans, a1 + a2);
  }
  bool n7 = s7;
  if (sum(7, xx, r - 1)) n7 = 1;
  int cur = fnd(l, xx, v * 2, p0, p1, n7);
  cur += sum(bad[p1 + 1] - '0', xx, r - 1);
  ans = min(ans, cur);
  return ans;
}
int rec(int l, int r, int a, int b, int v, int p0, int p1, bool s7) {
  if (p1 < 2) s7 = 1;
  if (p1 - p0 + 1 > r - l) return 1000000000;
  int &ans = mem[v][p0][p1][s7];
  if (iter[v][p0][p1][s7] != T) {
    ans = 1000000000 + 1;
    iter[v][p0][p1][s7] = T;
  }
  if (ans <= 1000000000) return ans;
  if (l == a && b == r - 1) {
    return ans = fnd(l, r, v, p0, p1, s7);
  }
  int xx = (r + l) / 2;
  if (b < xx) return ans = rec(l, xx, a, b, v * 2, p0, p1, s7);
  if (a >= xx) return ans = rec(xx, r, a, b, v * 2 + 1, p0, p1, s7);
  ans =
      sum(bad[p0] - '0', a, xx - 1) + rec(xx, r, xx, b, v * 2 + 1, p0, p1, s7);
  for (int _n(p1 - 1), i(p0); i <= _n; i++) {
    int a1 = rec(l, xx, a, xx - 1, v * 2, p0, i, s7);
    int a2 = rec(xx, r, xx, b, v * 2 + 1, i + 1, p1, s7);
    ans = min(ans, a1 + a2);
  }
  bool n7 = s7;
  if (sum(7, xx, b)) n7 = 1;
  int cur = rec(l, xx, a, xx - 1, v * 2, p0, p1, n7);
  cur += sum(bad[p1 + 1] - '0', xx, b);
  ans = min(ans, cur);
  return ans;
}
int main() {
  scanf("%d%d", &n, &q);
  memset(tr, 63, sizeof(tr));
  scanf("%s", str);
  for (int _n((n)-1), i(0); i <= _n; i++) {
    cs[str[i] - '0'][i] = 1;
  }
  for (int _n((10) - 1), i(0); i <= _n; i++) {
    for (int _n((n)-1), j(0); j <= _n; j++) {
      if (j) cs[i][j] += cs[i][j - 1];
    }
  }
  for (int _n((q)-1), i(0); i <= _n; i++) {
    ++T;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    int ans = rec(0, n, a, b, 1, 0, 2, 0);
    if (ans >= 1000000000) ans = -1;
    printf("%d\n", ans);
  }
}
