#include <bits/stdc++.h>
using namespace std;
bool SR(int &_x) { return scanf("%d", &_x) == 1; }
bool SR(long long &_x) { return scanf("%lld", &_x) == 1; }
bool SR(double &_x) { return scanf("%lf", &_x) == 1; }
bool SR(char *_s) { return scanf("%s", _s) == 1; }
bool RI() { return true; }
template <typename I, typename... T>
bool RI(I &_x, T &..._tail) {
  return SR(_x) && RI(_tail...);
}
void SP(const int _x) { printf("%d", _x); }
void SP(const long long _x) { printf("%lld", _x); }
void SP(const double _x) { printf("%.16lf", _x); }
void SP(const char *s) { printf("%s", s); }
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I _x, const T... _tail) {
  SP(_x);
  if (sizeof...(_tail)) putchar(' ');
  PL(_tail...);
}
const int maxn = 2e5 + 5;
int n, q;
char s[maxn];
void read() {
  RI(n, q);
  SR(s + 1);
}
int s20[maxn], s01[maxn], s167[maxn], s676[maxn];
int s2[maxn], s6[maxn];
void build() {
  for (int i = (1); i <= int(n); i++) s2[i] = s2[i - 1] + (s[i] == '2');
  for (int i = (1); i <= int(n); i++) s6[i] = s6[i - 1] + (s[i] == '6');
  for (int i = (1); i <= int(n); i++)
    s20[i] = s20[i - 1] + (s[i] == '2') - (s[i] == '0');
  for (int i = (1); i <= int(n); i++)
    s01[i] = s01[i - 1] + (s[i] == '0') - (s[i] == '1');
  for (int i = (1); i <= int(n); i++)
    s167[i] = s167[i - 1] + (s[i] == '1') - (s[i] == '6' || s[i] == '7');
  for (int i = (1); i <= int(n); i++)
    s676[i] = s676[i - 1] + (s[i] == '6' || s[i] == '7') - (s[i] == '6');
}
const int INF = 3e7;
struct Seg {
  struct Node {
    int dat[4][4];
    int *operator[](const int &i) { return dat[i]; }
    const int *operator[](const int &i) const { return dat[i]; }
    Node operator+(const Node &o) const {
      Node ret;
      for (int i = 0; i < int(4); i++)
        for (int j = (i); j <= int(3); j++) ret[i][j] = min(dat[i][j], o[i][j]);
      for (int i = 0; i < int(4); i++)
        for (int j = (i + 1); j <= int(3); j++)
          for (int k = (i); k <= int(j - 1); k++)
            ret[i][j] = min(ret[i][j], dat[i][k] + o[k + 1][j]);
      return ret;
    }
  } dat[maxn << 2];
  void init(int x, int low, int high) {
    if (low == high) {
      for (int i = 0; i < int(4); i++)
        for (int j = (i); j <= int(3); j++) dat[x][i][j] = INF;
      if (s[low] == '2') dat[x][0][0] = s20[low];
      if (s[low] == '0') dat[x][1][1] = s01[low];
      if (s[low] == '1') dat[x][2][2] = s167[low];
      if (s[low] == '7') dat[x][3][3] = s676[low];
      return;
    }
    int mid = (low + high) / 2;
    init(x << 1, low, mid);
    init(x << 1 | 1, mid + 1, high);
    dat[x] = dat[x << 1] + dat[x << 1 | 1];
  }
  Node ask(int x, int low, int high, int a, int b) {
    if (low == a && b == high) return dat[x];
    int mid = (low + high) / 2;
    if (b <= mid)
      return ask(x << 1, low, mid, a, b);
    else if (mid + 1 <= a)
      return ask(x << 1 | 1, mid + 1, high, a, b);
    else
      return ask(x << 1, low, mid, a, mid) +
             ask(x << 1 | 1, mid + 1, high, mid + 1, b);
  }
} seg;
void sol() {
  seg.init(1, 1, n);
  while (q--) {
    int l, r;
    RI(l, r);
    int ans = seg.ask(1, 1, n, l, r)[0][3] + s6[r] - s2[l - 1] - 4;
    if (ans <= n)
      PL(ans);
    else
      PL(-1);
  }
}
int main() {
  read();
  build();
  sol();
  return 0;
}
